Skip to content

ImagePreview 图片预览

介绍

支持全屏预览视频和图片

基础用法

html
<template>
    <nut-image-preview :show="showPreview" :images="imgData" @close="hideFn"/>
    <nut-cell isLink title="展示图片预览" :showIcon="true" @click="showFn"></nut-cell>
</template>

<script lang="ts">
  import { reactive, toRefs } from 'vue';
  export default {
    setup() {
        const resData = reactive({
            showPreview: false,
            imgData: [
                {
                    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',
                },
            ]
        });

        const showFn = () => {
            resData.showPreview = true;
        }

        const hideFn = () => {
            resData.showPreview = false;
        }

        return {
            ...toRefs(resData),
            showFn,
            hideFn
        };
    }
  };
</script>

设置初始页码

html
<template>
    <nut-image-preview :show="showPreview" :images="imgData" :init-no="3" @close="hideFn"/>
    <nut-cell isLink title="设置初始页码" :showIcon="true" @click="showFn"></nut-cell>
</template>
<script lang="ts">
  import { reactive, toRefs } from 'vue';
  export default {
    setup() {
        const resData = reactive({
            showPreview: false,
            imgData: [
                {
                    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',
                },
            ]
        });

        const showFn = () => {
            resData.showPreview = true;
        }

        const hideFn = () => {
            resData.showPreview = false;
        }

        return {
            ...toRefs(resData),
            showFn,
            hideFn
        };
    }
  };
</script>

设置轮播指示器及颜色

html
<template>
    <nut-image-preview
        :show="showPreview"
        :images="imgData"
        :pagination-visible="true"
        pagination-color="red"
        @close="hideFn"
    />
    <nut-cell isLink title="设置轮播指示器及颜色" :showIcon="true" @click="showFn"></nut-cell>
</template>
<script lang="ts">
  import { reactive, toRefs } from 'vue';
  export default {
    setup() {
        const resData = reactive({
            showPreview: false,
            imgData: [
                {
                    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',
                },
            ]
        });

        const showFn = () => {
            resData.showPreview = true;
        }

        const hideFn = () => {
            resData.showPreview = false;
        }

        return {
            ...toRefs(resData),
            showFn,
            hideFn
        };
    }
  };
</script>

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

小程序中,需要给这两个API:getImageInfo,saveImageToPhotosAlbum,设置隐私权限;网络图片需先配置download域名才能生效。

:::demo

html
<template>
  <nut-image-preview :show="showPreview" :images="imgData" @close="hideFn" @long-press="longPress" />
  <nut-cell isLink title="长按图片事件,保存到相册" :showIcon="true" @click="showFn"></nut-cell>
</template>

<script lang="ts" setup>
  import { reactive, toRefs } from 'vue';
  const resData = reactive({
    showPreview: false,
    imgData: [
      {
        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'
      }
    ]
  });
  const { showPreview, imgData } = toRefs(resData);
  const showFn = () => {
    resData.showPreview = true;
  };
  const hideFn = () => {
    resData.showPreview = false;
  };
  const longPress = (image: { src: string }) => {
    uni.getImageInfo({
      src: image.src,
      success: (res) => {
        uni.saveImageToPhotosAlbum({
          filePath: res.path,
          success: () => {
            uni.showToast({
              title: '保存成功'
            });
          }
        });
      }
    });
  };
</script>

API

Props

参数说明类型默认值
show是否展示预览图片booleanfalse
images预览图片数组ImageArray[][]
autoplay自动轮播时长,0 表示不会自动轮播number | string3000
init-no初始页码number0
pagination-visible分页指示器是否展示booleanfalse
pagination-color分页指示器选中的颜色string#fff
content-close点击图片可以退出预览booleantrue
show-index是否显示页码booleantrue
closeable是否显示关闭图标booleanfalse
close-icon-position关闭图标位置,可选值:top-leftstringtop-right
before-close关闭前的回调函数,返回 false 可阻止关闭,支持返回 Promise(active: number) => boolean | Promise<boolean>-
is-Loop是否循环播放booleantrue
scale v1.2.2是否支持双指缩放booleanfalse

ImageArray 数据结构

键名说明类型
src预览图片链接string

Events

事件名说明回调参数
close点击遮罩关闭图片预览时触发
change切换图片时触发index:当前图片索引
long-press 1.4.0小程序长按图片触发的事件(image: { src: string }) => void

Slots

名称说明
closeIcon自定义关闭图片

MIT Licensed