vue實(shí)現(xiàn)圖片預(yù)覽組件的封裝與使用

這是移動端使用vue框架與mint-ui實(shí)現(xiàn)的父用子之間的通信實(shí)現(xiàn)的圖片預(yù)覽的功能,在父組件中每一張圖片都可以實(shí)現(xiàn)圖片放大查看。

子組件

<!--html部分-->
<template>
    <div id="imgEnlarge" ref="imgEnlarge" class="img-bg" @click="imgBgHide" v-show="isShow">
          <mt-swipe :auto="0" :show-indicators="false" :continuous=false :defaultIndex="defaultIndex" @change="handleChange">
                <mt-swipe-item v-for="(item,index) in imgSrc" :key="index">
                      <img v-lazy="item" @click="handleClick"/>
                </mt-swipe-item>
          </mt-swipe>
    </div>
</template>
<!--js部分-->
<script>
    export default{
        data(){
            return{
                scroll:0
            }
        },
        props:{
            imgSrc:{
                type:Array
            },
            defaultIndex:{
                type:Number,
                default:0
            },
            isShow:{
                type:Boolean,
                deflault:false
            }
        },
        methods:{
            imgBgHide(){
                  this.$emit("imgBgHide") //向父組件傳遞事件
            } ,
            handleClick(e){
                  e.stopPropagation()//阻止事件冒泡,避免點(diǎn)擊預(yù)覽的圖片穿透遮罩層
            },
            handleChange(value){
            //向父組件傳遞輪播圖索引,解決加載圖片的問題
            this.$emit("handleChange",value)
            }
        },
      watch:{
            isShow:{//判斷遮罩是否顯示,顯示時底層頁面無法滾動,隱藏后滾動條回到顯示時的位置
                 handler(newVal,oldVal){
                    if(newVal==true){
                        this.scrolly = document.body.scrollTop;
                        document.body.style.position = "fixed";
                    }else{
                        document.body.style.position = "static";
                        document.body.scrollTop = this.scrolly; //
                    }
                } 
            }
      }

    }
</script>
<!--樣式部分-->
<style scoped>
      .img-bg {
            width:100%;
             height:100%;
             position:fixed;
             left:0;
             top:0;
             z-index:9999;
             background:rgba(0,0,0,1);
      }
      .img-bg img{
              width:auto;
              height:auto;
              max-width:100%;
              max-height:100%;
       }
</style>

父組件

<!--html部分-->
<template>
  <img-view :isShow="showImg" :imgSrc="imgSrc" @imgBgHide="imgBgHide" :deflaultIndex="defaultIndex" @handleChange="handleChange"></img-view>
</template>
<!--js部分-->
<script>
    //引入子組件
    import imgView from '@/components/common/imgEnlarge.vue';
    export default {
        data(){
            return{
                showImg:false,
                imgSrc:[],
                defaultIndex:0
            }
        },
        components:{imgView},
        mounted(){
            this.collectImgSrc()
        },
        methods:{
            imgBgHide(){//點(diǎn)擊遮罩層,遮罩層隱藏
                this.showImg = false;
            },
            handleChange(value){
                this.defaultIndex = value;
            },
            collectImgSrc(){//點(diǎn)擊圖片放大
                var _this = this;
                var src = document.getElementsByTagName("img");
                for(var i=0;i<src.length;i++){
                    _this.imgSrc.push(src[i].getAttribute("src"));
                    src[i].setAttribute("data-index",i);
                    src[i].onclick = function(e){
                        _this.showImg = true;
                        _this.defaultIndex = parseInt(e.target.getAttribute("data-index"));//設(shè)置初始顯示的輪播圖的索引
                    }
                }

            }
        }
    }
</script>

在全局樣式global.css里面設(shè)置圖片預(yù)覽居中

/*圖片點(diǎn)擊放大組件中swipe圖片居中*/
#imgEnlarge .mint-swipe-item-wrap > div {
    visibility:hidden;
    display:flex;
    -moz-box-pack:center;
    -webkit-box-pack:center;
    justify-content:center;
    -moz-box-align:center;
    -webkit-box-align:center;
    align-items:center;
    -webkit-align-items:center;
}
#imgEnlarge .mint-swipe-item-wrap > div.is-active {
    visibility:visible;
    display:flex;
    -moz-box-pack:center;
    -webkit-box-pack:center;
    justify-content:center;
    -moz-box-align:center;
    -webkit-box-align:center;
    align-items:center;
    -webkit-align-items:center;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基于Vue的組件庫 https://github.com/ElemeFE/element" element 餓了...
    _執(zhí)著執(zhí)著再執(zhí)著閱讀 32,965評論 4 230
  • 聽者是悲傷的袋子。聽者每天從不同的人聽屬于他們的悲傷故事。我就屬于聽者。有時他們未曾流淚,我早已淚流滿面...
    我是哦哦閱讀 381評論 1 0
  • 從前,有一個農(nóng)場,里面有許多可愛的小動物、大動物,其中有一只愛干凈的綿羊媽媽。羊媽媽總是把自己打理的一塵不染。...
    櫻花煙雨閱讀 979評論 1 1
  • 每到周末,不是我約女友,就是女友約我逛街,有時候,我會想:女人真是喜歡逛街。街上提著大包小包的基本上是...
    綠色點(diǎn)點(diǎn)灰閱讀 738評論 0 0

友情鏈接更多精彩內(nèi)容