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 相关文章推荐
cnblogs中在闪存中屏蔽某人的实现代码
Nov 14 Javascript
JavaScript之appendChild、insertBefore和insertAfter使用说明
Dec 30 Javascript
jQuery建立一个按字母顺序排列的友好页面索引(兼容IE6/7/8)
Feb 26 Javascript
jQuery制作的别致导航有阴影背景高亮模式窗口
Apr 15 Javascript
node.js中使用socket.io的方法
Dec 15 Javascript
javascript 动态生成css代码的两种方法
Mar 17 Javascript
node通过express搭建自己的服务器
Sep 30 Javascript
js实现图片局部放大效果详解
Mar 18 Javascript
基于 vue-skeleton-webpack-plugin 的骨架屏实战
Aug 05 Javascript
小程序实现多个选项卡切换
Jun 19 Javascript
微信小程序之高德地图多点路线规划过程示例详解
Jan 18 Javascript
Node与Python 双向通信的实现代码
Jul 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 !function_exists(&quot;T7FC56270E7A70FA81A5935B72EACBE29&quot;))代码解密
2011/01/07 PHP
PHP7 mongoDB扩展使用的方法分享
2019/05/02 PHP
在多个页面使用同一个HTML片段的代码
2011/03/04 Javascript
Package.js  现代化的JavaScript项目make工具
2012/05/23 Javascript
用jquery实现点击栏目背景色改变
2012/12/10 Javascript
js 利用image对象实现图片的预加载提高访问速度
2013/03/29 Javascript
获取内联和链接中的样式(js代码)
2013/04/11 Javascript
火狐下table中创建form导致两个table之间出现空白
2013/09/02 Javascript
javascript的渐进增强与平稳退化浅谈
2013/11/12 Javascript
Bootstrap框架下下拉框select搜索功能
2020/03/26 Javascript
用node和express连接mysql实现登录注册的实现代码
2017/07/05 Javascript
vue+socket.io+express+mongodb 实现简易多房间在线群聊示例
2017/10/21 Javascript
jQuery实现的简单对话框拖动功能示例
2018/06/05 jQuery
解决Vue2.0中使用less给元素添加背景图片出现的问题
2018/09/03 Javascript
支付宝小程序自定义弹窗dialog插件的实现代码
2018/11/30 Javascript
vue实现压缩图片预览并上传功能(promise封装)
2019/01/10 Javascript
送你43道JS面试题(收藏)
2019/06/17 Javascript
javascript网页随机点名实现过程解析
2019/10/15 Javascript
JavaScript console的使用方法实例分析
2020/04/28 Javascript
python中判断文件编码的chardet(实例讲解)
2017/12/21 Python
Python实现压缩文件夹与解压缩zip文件的方法
2018/09/01 Python
python 对字典按照value进行排序的方法
2019/05/09 Python
Python3+Appium安装使用教程
2019/07/05 Python
python 实现从高分辨图像上抠取图像块
2020/01/02 Python
详解基于Jupyter notebooks采用sklearn库实现多元回归方程编程
2020/03/25 Python
django实现日志按日期分割
2020/05/21 Python
基于python纯函数实现井字棋游戏
2020/05/27 Python
Python3开发环境搭建详细教程
2020/06/18 Python
如何基于Python实现word文档重新排版
2020/09/29 Python
实习销售业务员自我鉴定
2013/09/21 职场文书
工程专业毕业生自荐信范文
2013/12/25 职场文书
缓刑人员思想汇报500字
2014/09/12 职场文书
求职简历自荐信怎么写
2015/03/26 职场文书
超搞笑婚前保证书
2015/05/08 职场文书
2015中学政教处工作总结
2015/07/22 职场文书
应届生们该怎么书写求职信?
2019/07/05 职场文书