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 相关文章推荐
JSON 入门指南 想了解json的朋友可以看下
Aug 26 Javascript
JavaScript中的isXX系列是否继续使用的分析
Apr 16 Javascript
理解JSON:3分钟课程
Oct 28 Javascript
轻松实现javascript数据双向绑定
Nov 11 Javascript
JS动态增删表格行的方法
Mar 03 Javascript
Node.js模块封装及使用方法
Mar 06 Javascript
谈谈第三方App接入微信登录 解读
Dec 27 Javascript
Vue 项目中遇到的跨域问题及解决方法(后台php)
Mar 28 Javascript
vue单页面应用打开新窗口显示跳转页面的实例
Sep 21 Javascript
Vuejs学习笔记之使用指令v-model完成表单的数据双向绑定
Apr 29 Javascript
原生js实现抽奖小游戏
Jun 27 Javascript
在Vue中使用Viser说明(基于AntV-G2可视化引擎)
Oct 28 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令牌 Token改进版
2008/07/18 PHP
php实现MD5加密16位(不要默认的32位)
2013/08/12 PHP
php中ltrim()、rtrim()与trim()删除字符空格实例
2014/11/25 PHP
分享PHP函数实现数字与文字分页代码
2015/07/28 PHP
Yii2框架类自动加载机制实例分析
2018/05/02 PHP
java解析json方法总结
2019/05/16 PHP
用客户端js实现带省略号的分页
2013/04/27 Javascript
JQuery中对Select的option项的添加、删除、取值
2013/08/25 Javascript
javascript打印输出json实例
2013/11/11 Javascript
Jquery通过Ajax访问XML数据的小例子
2013/11/18 Javascript
jQuery对指定元素中指定字符串进行替换的方法
2015/03/17 Javascript
谈谈jQuery Ajax用法详解
2015/11/27 Javascript
javascript随机抽取0-100之间不重复的10个数
2016/02/25 Javascript
js浏览器html5表单验证
2016/10/17 Javascript
react-native封装插件swiper的使用方法
2018/03/20 Javascript
angularjs结合html5实现拖拽功能
2018/06/25 Javascript
vue中倒计时组件的实例代码
2018/07/06 Javascript
快速解决Vue项目在IE浏览器中显示空白的问题
2018/09/04 Javascript
15分钟学会vue项目改造成SSR(小白教程)
2019/12/17 Javascript
jQuery编写QQ简易聊天框
2020/08/27 jQuery
仅用500行Python代码实现一个英文解析器的教程
2015/04/02 Python
Python中字典创建、遍历、添加等实用操作技巧合集
2015/06/02 Python
python中装饰器级连的使用方法示例
2017/09/29 Python
深入理解Python分布式爬虫原理
2017/11/23 Python
基于django channel实现websocket的聊天室的方法示例
2019/04/11 Python
把Anaconda中的环境导入到Pycharm里面的方法步骤
2020/10/30 Python
python3访问字典里的值实例方法
2020/11/18 Python
HTML5制作表格样式
2016/11/15 HTML / CSS
加拿大最大的箱包及旅游配件零售商:Bentley Leathers
2017/07/19 全球购物
ASOS亚洲:ASOS Asia
2018/03/04 全球购物
FC-Moto美国:欧洲最大的摩托车服装和头盔商店之一
2019/08/24 全球购物
网络技术支持面试题
2013/04/22 面试题
高级文秘工作总结的自我评价
2013/09/28 职场文书
酒店采购员岗位职责
2014/03/14 职场文书
辞职信怎么写?你都知道吗?
2019/06/24 职场文书
一篇文章带你了解Python和Java的正则表达式对比
2021/09/15 Python