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
}
}
})
版权声明:本文为m0_37343874原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。