一、组件代码:
<template>
<div ref="maskBox" class="bigImgBox" @mousedown="onmousedown">
<img
:src="srcPath"
alt="加载失败"
:style="{'width': imgW + 'px','height': imgH + 'px','top': top + 'px', 'left': left + 'px', 'transform': scale}">
// 用来放置源图片
<img v-show="false" ref="resource" :src="srcPath" alt="">
</div>
</template>
<script>
export default {
props: ['srcPath'],
data() {
return {
imgW: 0,
imgH: 0,
top: 0,
left: 0,
src: '',
reW: 0,
reH: 0,
scale: 'scale(1)',
size: 0
}
},
mounted() {
this.reW = this.imgW = this.$refs.resource.width
this.reH = this.imgH = this.$refs.resource.height
const whRatio = this.reW / this.reH
const hwRatio = this.reH / this.reW
const bodyW = document.body.clientWidth
const bodyH = document.body.clientHeight
console.log(this.reW, this.reH, bodyW, bodyH)
if (this.reW >= this.reH) {
const nih = this.imgH = hwRatio * bodyW
if (nih > bodyH) {
this.imgH = bodyH
this.imgW = whRatio * bodyH
this.left = (bodyW - whRatio * bodyH) / 2
} else {
this.imgW = bodyW
this.top = (bodyH - nih) / 2
}
} else {
const niw = this.imgW = (bodyH / this.reH) * this.reW
this.imgH = bodyH
this.left = (bodyW - niw) / 2
}
const mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? 'DOMMouseScroll' : 'mousewheel'
this.$refs.maskBox.addEventListener(mousewheelevt, this.mWheel, false)
},
methods: {
hideMask(e) {
this.$emit('clickMask', false)
},
mWheel(e) {
const ev = e || window.event
const dir = ev.detail ? ev.detail * (-120) : ev.wheelDelta
if (dir > 0) {
this.size += dir / 2000
this.scale = `scale(${1 + this.size})`
} else {
this.size += dir / 2000
if (this.size < -1) {
this.size = -1
this.scale = `scale(${1 + this.size})`
return
}
this.scale = `scale(${1 + this.size})`
}
},
onmousedown(e) {
const that = this
let isMove5 = false
this.$refs.maskBox.onmousemove = function(el) {
const ev = el || window.event
ev.preventDefault()
if (Math.abs(ev.movementX) > 5 || Math.abs(ev.movementY) > 5) {
isMove5 = true
}
that.left += ev.movementX
that.top += ev.movementY
}
this.$refs.maskBox.onmouseup = function() {
if (that.isIE() || that.isIE11()) {
that.hideMask()
}
that.$refs.maskBox.onmousemove = null
that.$refs.maskBox.onmouseup = null
if (!isMove5) {
that.hideMask()
}
}
if (e.preventDefault) {
e.preventDefault()
} else {
return false
}
},
isIE() {
if (!!window.ActiveXObject || 'ActiveXObject' in window) {
return true
} else {
return false
}
},
isIE11() {
if ((/Trident\/7\./).test(navigator.userAgent)) {
return true
} else {
return false
}
}
}
}
</script>
<style>
.bigImgBox {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 9000;
background-color: rgba(0, 0, 0, .6)
}
.bigImgBox img {
position: fixed;
z-index: 9001;
}
</style>
二、组件使用
<!-- 需要require引入图片 -->
<div class="helloBox" @click="showBigImage(require('./common/earth.jpg'))" />
<pre-view v-if="isShow" :src-path="url" @clickMask="isShow = false" />
data(){
return {
url: '',
isShow: false,
}
}
showBigImage(url) {
if (url) {
this.url = url
this.isShow = true
}
},
三、学习思路
原文:blog.csdn.net/hbjiankely/…