小程序地图组件(map)中使用全屏预览图片(previewImage)的问题解决方案

小程序地图组件(map)中使用全屏预览图片(previewImage)的问题解决方案

背景

在小程序中 Map 组件中使用 previewImage 预览图片,无法在地图组件上预览图片,如果返回到上一页 previewImage 方法才会打开预览图片。

因为 map 组件是原生组件,层级默认最高,会覆盖在 WebView 之上。由于原生组件的隔离性,可能导致 previewImage 的调用被阻塞或延迟执行。

解决方案

方案一:弹窗打开预览图片

在地图组件上使用 cover-viewcover-image 等标签通过弹窗事项图片预览。

预览图片弹窗组件 使用 taro 框架为例: map 组件内的

index.jsx

jsx 复制代码
import { View, Swiper, SwiperItem, Image } from '@tarojs/components';
import classNames from 'classnames';

import styles from './index.module.scss';

const MapPreviewImage = (props) => {
  const { visible, onClose, images = [], current = 0 } = props;

  return (
    <View className={classNames(styles.imageModal, { [styles.visible]: visible })} onClick={onClose} catchMove>
      <Swiper
        current={current}
        className={styles.swiper}
        indicator-dots
        indicator-color="#fff"
        indicator-active-color="#DB5545"
      >
        {images?.map?.((image) => (
          <SwiperItem key={image} className={styles.swiperItem}>
            <View className={styles.imageWrapper}>
              <Image
                className={styles.image}
                mode="widthFix"
                src={image}
                onClick={(e) => {
                  e.stopPropagation();
                  e.preventDefault();
                }}
              />
            </View>
          </SwiperItem>
        ))}
      </Swiper>
    </View>
  );
};

export default MapPreviewImage;

index.module.scss

scss 复制代码
.image-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  z-index: 100;
  transition: all 0.3s ease-in-out;
  display: none;
  background-color: #000;

  &.visible {
    display: block;
  }

  .swiper {
    width: 100%;
    height: 100%;
  }

  .swiper-item {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
  }

  .image-wrapper {
    width: 100%;
    height: auto;
  }
}

使用预览图片弹窗组件 indx.jsx

javascript 复制代码
import { View, Map } from '@tarojs/components';
import MapPreviewImage from '../MapPreviewImage';

const Index = () => {
 const [visible, setVisible] = useState(false);
 const [previewImages, setPreviewImages] = useState([]); // img url

  return (
      <Map style={{height: '100vh'}} {...地图属性}>
        <View onClick={() => setVisible(true)}>打开弹窗</View>
          
        <MapPreviewImage 
            visible={visible} 
            onClose={() => setVisible(false)} 
            images={previewImages} />
      </Map>
  );
}

export default Index;

方案二:调整到新页面使用 previewImage 方法预览图片

  1. 定义一个新页面,props 需要有 previewImageurls; 在 useEefft 或者小程序的生命周期内调用 previewImage 方法打开预览图片;
  2. Map 组件页面,触发事件,跳转到预览图片页面并带需要的参数
  3. 此方案不具体赘述,不提供代码

注意事项

  1. Map 组件中的 View 使用 onClick 方法会有延迟,或可以使用 onTouchStart
  2. Map 组件内的其他元素滑动会带动地图滑动,可在元素上添加 catchMove 属性。
相关推荐
计算机魔术师12 分钟前
Meta突然杀进第一梯队:一个新模型,把AI推理成本打下来8倍
前端
IT_陈寒24 分钟前
Redis缓存雪崩把我坑惨了,这次长记性了
前端·人工智能·后端
风骏时光牛马35 分钟前
提示词工程:大模型效能挖掘与指令设计实战
前端
仓三37 分钟前
Chrome DevTools MCP 上手:让 Coding Agent 自己调试前端页面
前端·chrome devtools·mcp
计算机魔术师42 分钟前
扒完Google AI Agent挑战赛的前三名,我发现了一个共同点
前端
不可能片场1 小时前
resources/app 为何能覆盖 app.asar
前端·electron
小婉1 小时前
我用 Next.js + React Flow 从零搭建了一个可视化 AI 工作流编排平台
前端·人工智能·node.js
skiyee1 小时前
🚀 用 17 行代码给 UniApp 加上全局登录拦截
前端·uni-app
默_笙2 小时前
🌃 HTTP 不认识你:JWT 登录鉴权的完整"酒店入住"指南
前端·javascript
Profile排查笔记2 小时前
JavaScript 实现浏览器指纹生成:基础字段、Canvas 采样与 SHA-256 摘要
前端·人工智能·后端·自动化