vue实现图片预览组件封装与使用


Posted in Javascript onJuly 13, 2019

这是移动端使用vue框架与mint-ui实现的父用子之间的通信实现的图片预览的功能,在父组件中每一张图片都可以实现图片放大查看。

子组件

<!--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()//阻止事件冒泡,避免点击预览的图片穿透遮罩层
   },
   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(){//点击遮罩层,遮罩层隐藏
    this.showImg = false;
   },
   handleChange(value){
    this.defaultIndex = value;
   },
   collectImgSrc(){//点击图片放大
    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"));//设置初始显示的轮播图的索引
     }
    }

   }
  }
 }
</script>

在全局样式global.css里面设置图片预览居中

/*图片点击放大组件中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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript QueryString解析类代码
Jan 17 Javascript
javascript如何判断输入的url是否正确
Apr 11 Javascript
JavaScript获取网页表单action属性的方法
Apr 02 Javascript
javascript属性访问表达式用法分析
Apr 25 Javascript
JavaScript函数使用的基本教程
Jun 04 Javascript
jquery实现全屏滚动
Dec 28 Javascript
javascript实现简单的on事件绑定
Aug 23 Javascript
js 中文汉字转Unicode、Unicode转中文汉字、ASCII转换Unicode、Unicode转换ASCII、中文转换
Dec 06 Javascript
深入理解Angularjs向指令传递数据双向绑定机制
Dec 31 Javascript
javascript history对象详解
Feb 09 Javascript
在layui中select更改后生效的方法
Sep 05 Javascript
Vue vm.$attrs使用场景详解
Mar 08 Javascript
微信小程序基于Taro的分享图片功能实践详解
Jul 12 #Javascript
小程序实现悬浮搜索框
Jul 12 #Javascript
小程序实现搜索框功能
Mar 26 #Javascript
iview的table组件自带的过滤器实现
Jul 12 #Javascript
es6中比较有用的7个技巧小结
Jul 12 #Javascript
echarts大屏字体自适应的方法步骤
Jul 12 #Javascript
javascript实现简易聊天室
Jul 12 #Javascript
You might like
Zend引擎的发展 [15]
2006/10/09 PHP
php 输出json及显示json中的中文汉字详解及实例
2016/11/09 PHP
Javascript 布尔型分析
2008/12/22 Javascript
Prototype Template对象 学习
2009/07/19 Javascript
javascript 用原型继承来实现对象系统
2010/03/22 Javascript
JavaScript类和继承 prototype属性
2010/09/03 Javascript
基于jquery的3d效果实现代码
2011/03/23 Javascript
探索Emberjs制作一个简单的Todo应用
2012/11/07 Javascript
有关于eclipse配置spket需要注意的一些地方
2013/04/07 Javascript
jQuery实现鼠标划过修改样式的方法
2015/04/14 Javascript
深入浅出分析javaScript中this用法
2015/05/09 Javascript
javascript实现简单加载随机色方块
2015/12/25 Javascript
JavaScript省市区三级联动菜单效果
2016/09/21 Javascript
Vuejs 单文件组件实例详解
2018/02/09 Javascript
vue.js 图片上传并预览及图片更换功能的实现代码
2018/08/27 Javascript
一个小时快速搭建微信小程序的方法步骤
2019/04/15 Javascript
vue+elementui实现点击table中的单元格触发事件--弹框
2020/07/18 Javascript
[01:13:46]iG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
Python自动连接ssh的方法
2015/03/07 Python
python中itertools模块zip_longest函数详解
2018/06/12 Python
python装饰器代替set get方法实例
2019/12/19 Python
基于PyQT实现区分左键双击和单击
2020/05/19 Python
Keras自定义IOU方式
2020/06/10 Python
Python with语句用法原理详解
2020/07/03 Python
使用CSS3美化HTML表单的技巧演示
2016/05/17 HTML / CSS
李宁官方网店:中国运动品牌
2017/11/02 全球购物
LN-CC美国:伦敦时尚生活的缩影
2019/02/19 全球购物
AJAX都有哪些有点和缺点
2012/11/03 面试题
公共事业管理本科生求职信
2013/10/07 职场文书
人力资源主管的岗位职责
2014/03/15 职场文书
关于读书的演讲稿1000字
2014/08/27 职场文书
仲裁协议书
2014/09/26 职场文书
副总经理岗位职责范本
2014/09/30 职场文书
学习型家庭事迹材料(2016精选版)
2016/02/29 职场文书
如何用python反转图片,视频
2021/04/24 Python
一篇文章弄懂Python中的内建函数
2021/08/07 Python