html
<template>
<div v-if="popupVisible" class="image-back" @mousewheel.prevent="gunLun">
<div :style="imageItemToDisplay.data" class="image-inner" ref="imgWrap">
<i class="iconfont icon-cha11" @click="closeImage"></i>
<img
:src="imageItemToDisplay.url"
class="photo-popup-image"
id="oImage"
ref="image" alt=""
@mousedown.prevent="moveImg">
</div>
</div>
</template>
<script>
import {defineComponent, onMounted, reactive, ref} from 'vue';
export default defineComponent({
setup() {
const imageItemToDisplay = reactive({
url: '',
delta: 1,
data: ''
})
const image = ref(null)
const imgWrap = ref(null)
const gunLun = (e) => {
imageItemToDisplay.delta += e.wheelDelta / 1200;
if (imageItemToDisplay.delta > 0.1 && imageItemToDisplay.delta < 2.0) {
imageItemToDisplay.data = `transform:scale(${imageItemToDisplay.delta});`
}
}
const moveImg = (e) => {
let wrap = imgWrap.value
let img = image.value
let x = e.pageX - img.offsetLeft
let y = e.pageY - img.offsetTop
wrap.addEventListener('mousemove', move)
function move(e) {
wrap.style.left = e.pageX - x + 'px'
wrap.style.top = e.pageY - y + 'px'
}
img.addEventListener('mouseup', () => {
wrap.removeEventListener('mousemove', move)
})
wrap.addEventListener('mouseout', () => {
wrap.removeEventListener('mousemove', move)
})
}
return {
imageItemToDisplay,image,imgWrap,gunLun,moveImg
}
}
})
备注:部分借鉴https://cloud.tencent.com/developer/article/1926281