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 12 Javascript
Bootstrap零基础学习第一课之模板
Jul 18 Javascript
AngularJS基础 ng-copy 指令实例代码
Aug 01 Javascript
AngularJs验证重复密码的方法(两种)
Nov 25 Javascript
浅述Javascript的外部对象
Dec 07 Javascript
Node.js利用Net模块实现多人命令行聊天室的方法
Dec 23 Javascript
详解vue-cli 脚手架项目-package.json
Jul 04 Javascript
Angularjs中数据绑定的实例详解
Aug 25 Javascript
js中怎么判断两个字符串相等的实例
Jan 17 Javascript
EXTJS7实现点击拖拉选择文本
Dec 17 Javascript
微信小程序tab左右滑动切换功能的实现代码
Feb 08 Javascript
vue3使用vue-router的完整步骤记录
Jun 20 Vue.js
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基础之运算符的使用方法
2013/04/28 PHP
如何在php中正确的使用json
2013/08/06 PHP
PHP命令行脚本接收传入参数的三种方式
2014/08/20 PHP
smarty学习笔记之常见代码段用法总结
2016/03/19 PHP
php实现查询功能(数据访问)
2017/05/23 PHP
一个非常实用的php文件上传类
2017/07/04 PHP
利用PHPStorm如何开发Laravel应用详解
2017/08/30 PHP
Laravel框架Eloquent ORM简介、模型建立及查询数据操作详解
2019/12/04 PHP
Apply an AutoFormat to an Excel Spreadsheet
2007/06/12 Javascript
对之前写的jquery分页做下升级
2014/06/19 Javascript
AngularJS基础 ng-click 指令示例代码
2016/08/01 Javascript
AngularJS基础 ng-keypress 指令简单示例
2016/08/02 Javascript
通过AngularJS实现图片上传及缩略图展示示例
2017/01/03 Javascript
BootStrap table删除指定行的注意事项(笔记整理)
2017/02/05 Javascript
vuejs实现本地数据的筛选分页功能思路详解
2017/11/15 Javascript
Vue中v-for的数据分组实例
2018/03/07 Javascript
用p5.js制作烟花特效的示例代码
2018/03/21 Javascript
jQuery实现的别踩白块小游戏完整示例
2019/01/07 jQuery
JavaScript观察者模式原理与用法实例详解
2020/03/10 Javascript
ES6学习教程之Promise用法详解
2020/11/22 Javascript
Python base64编码解码实例
2015/06/21 Python
Python中几个比较常见的名词解释
2015/07/04 Python
Python中Class类用法实例分析
2015/11/12 Python
如何通过python画loss曲线的方法
2019/06/26 Python
python多进程并发demo实例解析
2019/12/13 Python
python爬取代理ip的示例
2020/12/18 Python
详解如何修改jupyter notebook的默认目录和默认浏览器
2021/01/24 Python
巴西葡萄酒商店:Divvino
2020/02/22 全球购物
餐饮业会计岗位职责
2013/12/19 职场文书
校园安全检查制度
2014/02/03 职场文书
大学生应聘求职信
2014/05/26 职场文书
财务助理岗位职责范本
2014/10/09 职场文书
民事赔偿协议书
2014/11/02 职场文书
事业单位年度考核个人总结
2015/02/12 职场文书
培训通知书模板
2015/04/17 职场文书
初三毕业感言
2015/07/31 职场文书