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 相关文章推荐
又一个图片自动缩小的JS代码
Mar 10 Javascript
33个优秀的jQuery 教程分享(幻灯片、动画菜单)
Jul 08 Javascript
jQuery.fn和jQuery.prototype区别介绍
Oct 05 Javascript
浅析js设置控件的readonly与enabled属性问题
Dec 25 Javascript
JavaScript window.location对象
Nov 14 Javascript
最简单的JavaScript图片轮播代码(两种方法)
Dec 18 Javascript
jquery中ajax跨域方法实例分析
Dec 18 Javascript
浅谈bootstrap使用中的一些问题以及解决过程
Oct 18 Javascript
如何实现json数据可视化详解
Nov 24 Javascript
JS高仿抛物线加入购物车特效实现代码
Feb 20 Javascript
解决修复npm安装全局模块权限的问题
May 17 Javascript
vue组件入门知识全梳理
Sep 21 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
Thinkphp结合ajaxFileUpload实现异步图片传输示例
2017/03/13 PHP
php基于自定义函数记录log日志方法
2017/07/21 PHP
jQuery 学习 几种常用方法
2009/06/11 Javascript
瀑布流布局并自动加载实现代码
2013/03/12 Javascript
返回上一页并自动刷新的JavaScript代码
2014/02/19 Javascript
javascript动态向网页中添加表格实现代码
2014/02/19 Javascript
js读取cookie方法总结
2014/10/31 Javascript
一个检测表单数据的JavaScript实例
2014/10/31 Javascript
JS获取浏览器语言动态加载JS文件示例代码
2014/10/31 Javascript
Js控制滑轮左右滑动实例
2015/02/13 Javascript
nodejs中实现sleep功能实例
2015/03/24 NodeJs
javascript实现淘宝幻灯片广告展示效果
2015/04/27 Javascript
轮播图组件js代码
2016/08/08 Javascript
基于jQuery实现的幻灯图片切换
2016/12/02 Javascript
JS实现针对给定时间的倒计时功能示例
2017/04/11 Javascript
js获取文件里面的所有文件名(实例)
2017/10/17 Javascript
JS实现判断图片是否加载完成的方法分析
2018/07/31 Javascript
JavaScript常用事件介绍
2019/01/21 Javascript
使用webpack搭建vue项目及注意事项
2019/06/10 Javascript
详解JWT token心得与使用实例
2019/08/02 Javascript
nodejs中使用archive压缩文件的实现代码
2019/11/26 NodeJs
javascript实现商品图片放大镜
2019/11/28 Javascript
vue实现拖拽进度条
2021/03/01 Vue.js
Python 元类使用说明
2009/12/18 Python
python中偏函数partial用法实例分析
2015/07/08 Python
Python 如何访问外围作用域中的变量
2016/09/11 Python
django ModelForm修改显示缩略图 imagefield类型的实例
2019/07/28 Python
Python lambda表达式filter、map、reduce函数用法解析
2019/09/11 Python
浅谈python之自动化运维(Paramiko)
2020/01/31 Python
Keras 使用 Lambda层详解
2020/06/10 Python
python 如何用urllib与服务端交互(发送和接收数据)
2021/03/04 Python
HTML5 解决苹果手机不能自动播放音乐问题
2017/12/27 HTML / CSS
小学教学随笔感言
2014/02/26 职场文书
学校社会实践活动总结
2014/07/03 职场文书
社会实践的活动方案
2014/08/22 职场文书
2016优秀员工先进事迹材料
2016/02/25 职场文书