Skip to content

ImagePreview 图片预览

介绍

支持全屏预览图片。

基础用法

html
<template>
  <nut-image-preview
    :show="show"
    :images="images"
    @close="onClose"
  ></nut-image-preview>
</template>
ts
const show = ref(false);

const images = ref([
  {
    src: "//m.360buyimg.com/mobilecms/s750x366_jfs/t1/18629/34/3378/144318/5c263f64Ef0e2bff0/0d650e0aa2e852ee.jpg"
  },
  {
    src: "//m.360buyimg.com/mobilecms/s750x366_jfs/t1/26597/30/4870/174583/5c35c5d2Ed55eedc6/50e27870c25e7a82.png"
  },
  {
    src: "//m.360buyimg.com/mobilecms/s750x366_jfs/t1/9542/17/12873/201687/5c3c4362Ea9eb757d/60026b40a9d60d85.jpg"
  },
  {
    src: "//m.360buyimg.com/mobilecms/s750x366_jfs/t1/30042/36/427/82951/5c3bfdabE3faf2f66/9adca782661c988c.jpg"
  }
]);

function preview() {
  show.value = true;
}

function onClose() {
  show.value = false;
}

设置初始页码

html
<template>
  <nut-image-preview
    :show="show"
    :images="images"
    :init-no="3"
    @close="onClose"
  ></nut-image-preview>
</template>

设置轮播指示器及颜色

html
<template>
  <nut-image-preview
    :show="show"
    :images="images"
    pagination-visible
    pagination-color="red"
    @close="onClose"
  ></nut-image-preview>
</template>

长按图片事件,保存到相册

注意,小程序端使用 saveImageToPhotosAlbum 需要用户授权, 详细内容请查看 官方文档

html
<template>
  <nut-image-preview
    :show="show"
    :images="images"
    @long-press="onLongPress"
    @close="onClose"
  ></nut-image-preview>
</template>
ts
function onLongPress(image: { src: string }) {
  uni.getImageInfo({
    src: image.src,
    success(res) {
      uni.saveImageToPhotosAlbum({
        filePath: res.path,
        success() {
          uni.showToast({
            title: "保存成功"
          });
        }
      });
    }
  });
}

API

Props

参数说明类型可选值默认值
show是否展示预览图片boolean-false
images预览图片数组Array-[]
autoplay自动轮播时长(0 表示不会自动轮播)number / string-3000
init-no初始页码number-0
pagination-visible分页指示器是否展示boolean-false
pagination-color分页指示器选中的颜色string-#fff
content-close点击图片可以退出预览boolean-true
show-index是否显示页码boolean-true
closeable是否显示关闭图标boolean-false
close-icon-position关闭图标位置stringtop-left / top-righttop-right
before-close关闭前的回调函数(返回 false 可阻止关闭,支持返回 PromiseFunction--
is-loop是否循环播放boolean-true
scale 1.2.2是否支持双指缩放boolean-false
show-menu-by-longpress 1.9.4是否启用小程序长按菜单boolean-false

图片数组数据结构

参数说明类型
src预览图片链接string

Events

事件名说明类型
close点击遮罩关闭图片预览时触发() => void
change切换图片时触发(index: number) => void
long-press 1.4.0长按图片触发的事件(image: { src: string }) => void

MIT Licensed