js 基于element实现动态调用挂载实例图片预览功能

上代码:

import Vue from 'vue'
export default async function(urlList: string[], index = 0) {
    const ImageViewer = await import('element-ui/packages/image/src/image-viewer.vue')
    /** 创建响应式数据 */
    const data = Vue.observable({
        index,
        hide: false,
    })
    /** 创建虚拟节点 */
    const vm = new Vue({
        // 这里用的h 写法(看不懂可以去了解一下 vue render 的createdElement),也可以使用jsx,或者动态引入vue组件
        render: h =>
            h(
                'div',
                {
                    class: ['ImageViewerBox', style['ImageViewerBox'], { [style['ImageViewerBoxLeave']]: data.hide }],
                },
                [
                    h(ImageViewer.default, {
                        props: { urlList, initialIndex: data.index, onClose, onSwitch, zIndex: 9999 },
                        ref: 'ImageViewer',
                        style: {
                            bottom: `${urlList?.length > 1 ? 105 : 0}px`,
                        },
                    }),
                    h(
                        'ul',
                        {
                            class: [style['contraction-box']],
                            style: {
                                'animation-direction': data.hide ? 'reverse' : 'normal',
                            },
                        },
                        [
                            urlList?.length > 1 &&
                                urlList.map((i, index) =>
                                    h(
                                        'li',
                                        {
                                            key: index,
                                            class: [style['li'], { [style['li-show']]: index === data.index }],
                                            on: {
                                                click: () => upIndex(index),
                                            },
                                        },
                                        [
                                            h('img', {
                                                class: style['img'],
                                                attrs: {
                                                    src: i,
                                                },
                                            }),
                                        ],
                                    ),
                                ),
                        ],
                    ),
                ],
            ),
    }).$mount()
    /** 挂载实例 */
    document.querySelector('body')?.appendChild?.(vm.$el)
    /** 销毁组件 */
    function onClose() {
        setTimeout(() => {
            vm.$destroy()
            document.querySelector('body')?.removeChild(vm.$el)
        }, 280)
        data.hide = true
    }
    /** 监听组件index值变化 */
    function onSwitch(index) {
        data.index = index
    }
    /** 更新组件index */
    function upIndex(index) {
        ;(vm.$refs.ImageViewer as Vue).$data.index = index
    }
}

/** 
 * 使用方法:
 * $ImageViewer([url],index)
 * url 图片链接(以数组形式传入,单个[url],多个[url1,url2,url3])
 * index 用于传入多个url时,初始显示图片的下标,不传默认0
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值