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 跨域访问解决方案
Feb 14 Javascript
JavaScript创建一个欢迎cookie弹出窗实现代码
Mar 15 Javascript
解析javascript 实用函数的使用详解
May 10 Javascript
JavaScript调用客户端的可执行文件(示例代码)
Nov 28 Javascript
jQuery中(function($){})(jQuery)详解
Jul 15 Javascript
深入理解js数组的sort排序
May 28 Javascript
模拟javascript中的sort排序(简单实例)
Aug 17 Javascript
jquery表单提交带错误信息提示效果
Mar 09 Javascript
使用Require.js封装原生js轮播图的实现代码
Jun 15 Javascript
用webpack4开发小程序的实现方法
Jun 04 Javascript
微信小程序封装分享与分销功能过程解析
Aug 13 Javascript
JS实现iframe中子父页面跨域通讯的方法分析
Mar 10 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无敌近乎加密方式!
2010/07/17 PHP
PHP查询快递信息的方法
2015/03/07 PHP
php+jQuery+Ajax简单实现页面异步刷新
2016/08/08 PHP
PHP实现表单提交数据的验证处理功能【防SQL注入和XSS攻击等】
2017/07/21 PHP
Laravel框架运行出错提示RuntimeException No application encryption key has been specified.解决方法
2019/04/02 PHP
javascript获得服务器端控件的ID的实现代码
2011/12/28 Javascript
父元素与子iframe相互获取变量和元素对象的具体实现
2013/10/15 Javascript
jQuery中trigger()与bind()用法分析
2015/12/18 Javascript
js+flash实现的5图变换效果广告代码(附演示与demo源码下载)
2016/04/01 Javascript
jquery 无限极下拉菜单的简单实例(精简浓缩版)
2016/05/31 Javascript
jQuery中DOM节点的删除方法总结(超全面)
2017/01/22 Javascript
jQuery常用选择器详解
2017/07/17 jQuery
JS 实现百度搜索功能
2018/02/01 Javascript
express框架中使用jwt实现验证的方法
2019/08/25 Javascript
Windows上node.js的多版本管理工具用法实例分析
2019/11/06 Javascript
jQuery实现高度灵活的表单验证功能示例【无UI】
2020/04/30 jQuery
vue 使用post/get 下载导出文件操作
2020/08/07 Javascript
Python正则表达式的使用范例详解
2014/08/08 Python
分析Python编程时利用wxPython来支持多线程的方法
2015/04/07 Python
浅析Python中的getattr(),setattr(),delattr(),hasattr()
2016/06/14 Python
python生成excel的实例代码
2017/11/08 Python
Python实现 版本号对比功能的实例代码
2019/04/18 Python
梅尔倒谱系数(MFCC)实现
2019/06/19 Python
在Python函数中输入任意数量参数的实例
2019/07/16 Python
浅谈pytorch torch.backends.cudnn设置作用
2020/02/20 Python
css3动画 小球滚动 js控制动画暂停
2019/11/29 HTML / CSS
canvas 阴影和图形变换的示例代码
2018/01/02 HTML / CSS
作为网站管理者应当如何防范XSS
2014/08/16 面试题
自主招生自荐信范文
2013/12/04 职场文书
民主评议党员自我评价材料
2014/09/18 职场文书
趵突泉导游词
2015/02/03 职场文书
家装业务员岗位职责
2015/04/03 职场文书
2015年电话客服工作总结
2015/05/18 职场文书
中学图书馆工作总结
2015/08/11 职场文书
LayUI+Shiro实现动态菜单并记住菜单收展的示例
2021/05/06 Javascript
CSS使用SVG实现动态分布的圆环发散路径动画
2022/12/24 HTML / CSS