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 一个函数对同一元素的多个事件响应
Jul 25 Javascript
关于jQuery参考实例 1.0 jQuery的哲学
Apr 07 Javascript
js获得指定控件输入光标的坐标兼容IE,Chrome,火狐等多种主流浏览器
May 21 Javascript
js中indexof的用法详细解析
Dec 24 Javascript
JQuery鼠标移到小图显示大图效果的方法
Jun 10 Javascript
JS判断图片是否加载完成方法汇总(最新版)
May 13 Javascript
Google 爬虫如何抓取 JavaScript 的内容
Apr 07 Javascript
解决Extjs下拉框不显示的问题
Jun 21 Javascript
Vue侧滑菜单组件——DrawerLayout
Dec 18 Javascript
vue-cli实现多页面多路由的示例代码
Jan 30 Javascript
vue2.0+koa2+mongodb实现注册登录
Apr 10 Javascript
node puppeteer(headless chrome)实现网站登录
May 09 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
使用 MySQL Date/Time 类型
2008/03/26 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
2014/06/25 PHP
Thinkphp使用mongodb数据库实现多条件查询方法
2014/06/26 PHP
php获取ajax的headers方法与内容实例
2017/12/27 PHP
php设计模式之享元模式分析【星际争霸游戏案例】
2020/03/23 PHP
php中get_object_vars()在数组的实例用法
2021/02/22 PHP
学习ExtJS 访问容器对象
2009/10/07 Javascript
JQuery实现的按钮倒计时效果
2015/12/23 Javascript
深入理解angular2启动项目步骤
2017/07/15 Javascript
基于Vue实例对象的数据选项
2017/08/09 Javascript
bootstrap模态框嵌套、tabindex属性、去除阴影的示例代码
2017/10/17 Javascript
详解Chai.js断言库API中文文档
2018/01/31 Javascript
JavaScript防止全局变量污染的方法总结
2018/08/02 Javascript
element-ui 表格数据时间格式化的方法
2018/08/24 Javascript
在vue中更换字体,本地存储字体非引用在线字体库的方法
2018/09/28 Javascript
微信小程序实现签到功能
2018/10/31 Javascript
Typescript 中的 interface 和 type 到底有什么区别详解
2019/06/18 Javascript
webstorm建立vue-cli脚手架的傻瓜式教程
2020/09/22 Javascript
JavaScript实现弹出窗口效果
2020/12/09 Javascript
详解阿里Node.js技术文档之process模块学习指南
2021/01/04 Javascript
python冒泡排序算法的实现代码
2013/11/21 Python
python&amp;MongoDB爬取图书馆借阅记录
2016/02/05 Python
Python学习小技巧之列表项的拼接
2017/05/20 Python
django实现前后台交互实例
2017/08/07 Python
Python设计模式之MVC模式简单示例
2018/01/10 Python
Numpy截取指定范围内的数据方法
2018/11/14 Python
对Python中画图时候的线类型详解
2019/07/07 Python
Python3操作读写CSV文件使用包过程解析
2020/04/10 Python
PyTorch-GPU加速实例
2020/06/23 Python
浅谈HTML5新增和废弃的标签
2019/04/28 HTML / CSS
《欢乐的泼水节》教学反思
2014/04/22 职场文书
学生会竞选演讲稿
2014/04/24 职场文书
小兵张嘎电影观后感
2015/06/03 职场文书
辩论赛新闻稿
2015/07/17 职场文书
2016教师廉洁从教心得体会
2016/01/13 职场文书
《西门豹》教学反思
2016/02/23 职场文书