vue.js实现双击放大预览功能


Posted in Javascript onJune 23, 2020

本文实例为大家分享了vue.js实现双击放大预览的具体代码,供大家参考,具体内容如下

vue.js实现双击放大预览功能

vue.js实现双击放大预览功能

imgPreview组件

<template>
 <div class="vue-uploader" @keyup.esc.native="hide">
 <div v-if="visible" @click.self="hide" class="img_model" >
 <div class="img-btn btn-pre" @click="preImg" v-show="imgList.length>1"><i class="el-icon-arrow-left"></i></div>
 <div class="img-btn btn-next" @click="nextImg" v-show="imgList.length>1"><i class="el-icon-arrow-right"></i></div>
 <div class="center-box">
 <div class="img_box" v-bind:style="{ transform: 'rotate(' + deg + 'deg)' }">
  <img :src="imgList[imgIndex]" alt="" id="oImg" @click="e=>e.stopPropagation()" v-bind:style="{ zoom: zoom }">
 </div>
 </div>
 <div @click="e=>e.stopPropagation()" class="btns">
 <img src="https://static-frontpicture.lexing360.com/min.png" @click="imgToSize(false)">
 <img src="https://static-frontpicture.lexing360.com/rotate.png" @click="rotate">
 <img src="https://static-frontpicture.lexing360.com/plus.png" @click="imgToSize(true)">
 </div>
 </div>
 </div>
</template>
<script>
 export default {
 props: {
 initImgIndex: {
 required: true
 },
 imgList: {
 required: true,
 },
 visible: {
 required: true
 },
 },
 data() {
 return {
 src: '',
 pasteText: '',
 zoom: '100%',
 imgIndex: 0,
 deg: 0,
 firstTag: true
 }
 },
 created () {
 this.imgIndex = this.initImgIndex
 },
 watch: {
 visible(val) {
 this.imgIndex = this.initImgIndex
 this.zoom = '100%'
 this.firstTag = true
 this.$emit('update:visible', val);
 if (val) {
  this.$emit('open');
 } else {
  this.$el.removeEventListener('scroll', this.updatePopper);
  this.$emit('close');
 }
 }
 },
 methods: {
 imgToSize(oBool) {
 if (this.firstTag && !oBool && document.getElementById('oImg') && document.getElementById('oImg').naturalWidth > window.innerWidth) {
  this.zoom = parseInt(window.innerWidth * 0.9 / document.getElementById('oImg').naturalWidth * 100) + '%'
  this.firstTag = false
 }
 if ((document.getElementById('oImg').width * parseInt(this.zoom) / 100 <= 200 || this.zoom == '2%') && !oBool) {
  this.$message.info('已经最小了!')
  return
 }
 if (document.getElementById('oImg') && document.getElementById('oImg').x <= window.innerWidth * 0.05 && oBool) {
  this.$message.info('已经最大了!')
  return
 }
 this.zoom = parseInt(this.zoom) + (oBool ? 2 : -2) + '%'; 
 },
 rotate () {
 this.deg += 90
 },
 nextImg (e) {
 e.stopPropagation()
 if (this.imgIndex == this.imgList.length-1) {
  this.imgIndex = 0
 } else {
  this.imgIndex ++
 }

 },
 preImg(e) {
 e.stopPropagation()
 if (this.imgIndex == 0) {
  this.imgIndex = this.imgList.length - 1
 } else {
  this.imgIndex --
 }
 },
 
 hide (cancel) {
 if (cancel !== false) {
  this.$emit('update:visible', false);
  this.$emit('visible-change', false);
 }
 },
 }
 }
</script>
<style>
 .img_model{
 position: fixed;
 width: 100%;
 min-height: 100%;
 background: rgba(0,0,0,.5);
 top: 0;
 left: 0;
 z-index: 9999;
 /* text-align: center; */
 display: flex;
 flex-direction: column;
 justify-content: center;
 align-items: center; 
 }
 .center-box {
 position: relative;
 max-width: 90%;
 }
 .img_model .img_box {
 max-width: 100%;
 max-height: 800px;
 overflow-y: scroll;
 }
 .img_model .img_box::-webkit-scrollbar {
 display: none;
}
 .img_model .img_box img{
 max-width: 100%;
 }
 .img_model .img-btn {
 position: absolute;
 top: 50%;
 transform: translateY(-50%);
 z-index: 100;
 width: 50px;
 height: 70px;
 font-size: 30px;
 line-height: 70px;
 text-align: center;
 background: rgba(255, 255, 255, .3);
 color: #FFF;
 }
 .img_model .btn-pre {
 left: 5%;
 }
 .img_model .btn-next {
 right: 5%;
 }
 .img_model .img_box .btn-next {
 right: 20rpx;
 }
 .img_model .btns{
 position: fixed;
 bottom: 25px;
 -webkit-user-select:none;
 -moz-user-select:none;
 -ms-user-select:none;
 user-select:none;
 }
</style>

引入这个组件

import imgPreview from './imgPreview'
 data:{
 return{
 bigImgShow: false,
 bigImgIndex:'',
 imgList:[],
 }
 }
 components: {
 imgPreview
 }, 
 method:{
 previewImage (imgList, index) {
 if (imgList) {
  this.imgList = imgList
  this.bigImgIndex = index
  this.bigImgShow = true
 }
 },
 }

template里面渲染

<imgPreview :visible.sync="bigImgShow" :initImgIndex="bigImgIndex" :imgList="imgList"></imgPreview>

关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。

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

Javascript 相关文章推荐
jquery异步循环获取功能实现代码
Sep 19 Javascript
js 链式延迟执行DOME
Jan 04 Javascript
JS无限极树形菜单,json格式、数组格式通用示例
Jul 30 Javascript
使用js修改客户端注册表的方法
Aug 09 Javascript
JavaScript作用域与作用域链深入解析
Dec 06 Javascript
JQuery each()嵌套使用小结
Apr 18 Javascript
漫谈JS引擎的运行机制 你应该知道什么
Jun 15 Javascript
javascript设计模式之module(模块)模式
Aug 19 Javascript
VUE 更好的 ajax 上传处理 axios.js实现代码
May 10 Javascript
jQuery实现验证表单密码一致性及正则表达式验证邮箱、手机号的方法
Dec 05 jQuery
实时监控input框,实现输入框与下拉框联动的实例
Jan 23 Javascript
js实现点赞效果
Mar 16 Javascript
vue实现分页的三种效果
Jun 23 #Javascript
微信小程序清空输入框信息与实现屏幕往上滚动的示例代码
Jun 23 #Javascript
weui上传多图片,压缩,base64编码的示例代码
Jun 22 #Javascript
详细分析Node.js 多进程
Jun 22 #Javascript
详细分析vue响应式原理
Jun 22 #Javascript
Vue循环遍历选项赋值到对应控件的实现方法
Jun 22 #Javascript
如何解决jQuery 和其他JS库的冲突
Jun 22 #jQuery
You might like
PHP限制页面只能在微信自带浏览器访问的代码
2014/01/15 PHP
PHP实现bitmap位图排序与求交集的方法
2016/07/28 PHP
Laravel jwt 多表(多用户端)验证隔离的实现
2019/12/18 PHP
filemanage功能中用到的lib.js
2007/04/08 Javascript
缓动函数requestAnimationFrame 更好的实现浏览器经动画
2012/12/07 Javascript
javascript实现动态侧边栏代码
2014/02/19 Javascript
使用js画图之圆、弧、扇形
2015/01/12 Javascript
js拖拽的原型声明和用法总结
2016/04/04 Javascript
返回函数的JavaScript函数
2016/06/14 Javascript
Bootstrap的modal拖动效果
2016/12/25 Javascript
详解React 服务端渲染方案完美的解决方案
2018/12/14 Javascript
JS实现水平遍历和嵌套递归操作示例
2019/08/15 Javascript
Javascript原型链及instanceof原理详解
2020/05/25 Javascript
解决vue-pdf查看pdf文件及打印乱码的问题
2020/11/04 Javascript
让python同时兼容python2和python3的8个技巧分享
2014/07/11 Python
python获取微信小程序手机号并绑定遇到的坑
2018/11/19 Python
Python查找最长不包含重复字符的子字符串算法示例
2019/02/13 Python
Python爬虫图片懒加载技术 selenium和PhantomJS解析
2019/09/18 Python
如何通过python实现全排列
2020/02/11 Python
tensorflow模型的save与restore,及checkpoint中读取变量方式
2020/05/26 Python
video实现有声音自动播放的实现方法
2020/05/20 HTML / CSS
中国跨境电子商务网站:NewFrog
2018/03/10 全球购物
Hotels.com拉丁美洲:从豪华酒店到经济型酒店的预定优惠和折扣
2019/12/09 全球购物
同步和异步有何异同,在什么情况下分别使用他们
2013/04/09 面试题
工程师自我评价怎么写
2013/09/19 职场文书
女儿十岁生日答谢词
2014/01/27 职场文书
求职信范文怎么写
2014/01/29 职场文书
知识竞赛拉拉队口号
2014/06/16 职场文书
承租经营合作者协议书
2014/10/01 职场文书
2014年纳税评估工作总结
2014/12/23 职场文书
管理人员岗位职责
2015/02/14 职场文书
师范生见习总结范文
2015/06/23 职场文书
2015年社区党建工作汇报材料
2015/06/25 职场文书
少先队入队仪式主持词
2015/07/04 职场文书
结婚喜宴迎宾词
2015/08/10 职场文书
教你用Python matplotlib库制作简单的动画
2021/06/11 Python