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 相关文章推荐
javascript-TreeView父子联动效果保持节点状态一致
Aug 12 Javascript
JQuery each打印JS对象的方法
Nov 13 Javascript
用jquery.sortElements实现table排序
May 04 Javascript
jQuery的end()方法使用详解
Jul 15 Javascript
js基于cookie记录来宾姓名的方法
Jul 19 Javascript
JS获取url参数、主域名的方法实例分析
Aug 03 Javascript
JS识别浏览器类型(电脑浏览器和手机浏览器)
Nov 18 Javascript
jQuery表格(Table)基本操作实例分析
Mar 10 Javascript
Javascript循环删除数组中元素的几种方法示例
May 18 Javascript
jQuery 开发之EasyUI 添加数据的实例
Sep 26 jQuery
CKeditor富文本编辑器使用技巧之添加自定义插件的方法
Jun 14 Javascript
JS简单表单验证功能完整示例
Jan 26 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预定义常量
2006/12/25 PHP
php截取字符串函数分享
2015/02/02 PHP
完美解决thinkphp唯一索引重复时出错的问题
2017/03/31 PHP
excel操作之Add Data to a Spreadsheet Cell
2007/06/12 Javascript
JQuery Tips(2) 关于$()包装集你不知道的
2009/12/14 Javascript
新手常遇到的一些jquery问题整理
2010/08/16 Javascript
jQuery中bind与live的用法及区别小结
2014/01/27 Javascript
jQuery随机密码生成的方法
2015/03/09 Javascript
jQuery提示插件alertify使用指南
2015/04/21 Javascript
JS+CSS实现类似QQ好友及黑名单效果的树型菜单
2015/09/22 Javascript
基于JS+Canves实现点击按钮水波纹效果
2016/09/15 Javascript
再谈javascript常见错误及解决方法
2016/09/16 Javascript
jQuery插件HighCharts绘制2D饼图效果示例【附demo源码下载】
2017/03/21 jQuery
详解Angular的8个主要构造块
2017/06/20 Javascript
js实现图片放大展示效果
2017/08/30 Javascript
vue - vue.config.js中devServer配置方式
2019/10/30 Javascript
Vue router传递参数并解决刷新页面参数丢失问题
2020/12/02 Vue.js
vue 项目@change多个参数传值多个事件的操作
2021/01/29 Vue.js
[02:38]DOTA2 夜魇暗潮2020活动介绍官方视频
2020/11/04 DOTA
matplotlib作图添加表格实例代码
2018/01/23 Python
python实现决策树分类(2)
2018/08/30 Python
Django 路由系统URLconf的使用
2018/10/11 Python
对Python3中bytes和HexStr之间的转换详解
2018/12/04 Python
Django 用户认证组件使用详解
2019/07/23 Python
python Elasticsearch索引建立和数据的上传详解
2019/08/04 Python
django创建超级用户过程解析
2019/09/18 Python
tensorflow实现读取模型中保存的值 tf.train.NewCheckpointReader
2020/02/10 Python
python json 递归打印所有json子节点信息的例子
2020/02/27 Python
Python 列表中的修改、添加和删除元素的实现
2020/06/11 Python
思想政治自我鉴定
2013/10/06 职场文书
科研先进个人典型材料
2014/01/31 职场文书
煤矿机修工岗位职责
2014/02/07 职场文书
大学生社会实践评语
2014/04/25 职场文书
小学生倡议书范文
2014/05/13 职场文书
带你了解CSS基础知识,样式
2021/07/21 HTML / CSS
Python 中的Sympy详细使用
2021/08/07 Python