一个基于vue的类似于图片拖放 鼠标为中心点缩放的单文件组件

<template>

<div class='test' ref='testDivDom' style=" position: relative;left: 100px;top: 100px;border:1px dashed black;width: 500px;height: 500px;overflow: hidden">

<!-- <img src='../assets/map.jpg' alt="" class='div_img' @mousedown="dragImg" ref='dragImgDom' style='width: 1000px;top: 100px;height: 1000px;position: absolute;left: 100px;'> -->

<div class='div_img' @mousedown="dragImg" ref='dragImgDom' @mousewheel="fnWheel" style='width: 1000px;top: 100px;height: 1000px;position: absolute;left: 100px;'></div>

</div>

</template>

<script>

export default {

name: 'helloWorld',

data () {

return {

mouseLeft: 0,

mouseTop: 0,

curX: 0,

curY: 0,

dragFlag: false,

wheelFlag: null,

oldWidth: 0,

oldHeight: 0,

scaleX: 1,

scaleY: 1,

newWidth: 0,

newHeight: 0,

x: 0,

y: 0,

bgX: 0,

bgY: 0,

ww: null,

wh: null,

imgw: null,

imgh: null,

scaleSize: null,

testDivDom: null,

dragImgDom: null,

w: null,

h: null,

i: null

}

},

mounted () {

this.testDivDom = this.$refs.testDivDom

this.dragImgDom = this.$refs.dragImgDom

this.ww = parseInt(this.testDivDom.style.width)

this.wh = parseInt(this.testDivDom.style.height)

this.imgw = parseInt(this.dragImgDom.style.width)

this.imgh = parseInt(this.dragImgDom.style.height)

if (this.ww / this.wh < this.imgw / this.imgh) {

this.w = this.ww

this.h = this.imgh * this.ww / this.imgw

this.bgX = 0

this.bgY = -(this.h - this.wh) / 2

this.scaleSize = this.ww / this.imgw

} else {

this.w = this.imgw * this.wh / this.imgh

this.h = this.wh

this.bgX = -(this.w - this.ww) / 2

this.bgY = 100

this.scaleSize = this.wh / this.imgh

}

this.dragImgDom.style.width = this.w + 'px'

this.dragImgDom.style.height = this.h + 'px'

this.dragImgDom.style.left = this.bgX + 'px'

this.dragImgDom.style.top = this.bgY + 'px'

},

methods: {

getOffset (o) {

var left = 0

var top = 0

while (o != null && o !== document.body) {

top += o.offsetTop

left += o.offsetLeft

o = o.offsetParent

}

return {

left: left,

top: top

}

},

dragImg (e) {

this.dragFlag = true

this.mouseLeft = e.clientX - parseInt(this.$refs.dragImgDom.offsetLeft)

this.mouseTop = e.clientY - parseInt(this.$refs.dragImgDom.offsetTop)

document.onmousemove = (e) => {

if (this.dragFlag) {

this.curX = e.clientX - this.mouseLeft

this.curY = e.clientY - this.mouseTop

this.$refs.dragImgDom.style.left = this.curX + 'px'

this.$refs.dragImgDom.style.top = this.curY + 'px'

console.log(this.curX)

}

}

document.onmouseup = () => {

this.dragFlag = false

}

},

fnWheel (e) {

// 思路:以鼠标为中心实现滚动的话,那么将会鼠标在背景图片(注意我这里是用背景图片的,不是img的)中滚动的时候的位置比率是不会变的

if (e.wheelDelta > 0) {

this.wheelFlag = true

} else {

this.wheelFlag = false

}

this.oldWidth = this.$refs.dragImgDom.offsetWidth

this.oldHeight = this.$refs.dragImgDom.offsetHeight

this.mouseLeft = e.clientX - this.$refs.dragImgDom.offsetLeft - parseInt(this.$refs.testDivDom.offsetLeft)

this.mouseTop = e.clientY - this.$refs.dragImgDom.offsetTop - parseInt(this.$refs.testDivDom.offsetTop)

// 分别计算出scaleX,scaleY的倍数

this.scaleX = this.mouseLeft / this.oldWidth

this.scaleY = this.mouseTop / this.oldHeight

if (this.wheelFlag) {

this.$refs.dragImgDom.style.width = this.$refs.dragImgDom.offsetWidth * 1.05 + 'px'

this.$refs.dragImgDom.style.height = this.$refs.dragImgDom.offsetHeight * 1.05 + 'px'

if (parseInt(this.$refs.dragImgDom.style.width) > 2500) {

this.$refs.dragImgDom.style.width = '2500px'

}

if (parseInt(this.$refs.dragImgDom.style.height) > 1500) {

this.$refs.dragImgDom.style.height = '1500px'

}

} else {

this.$refs.dragImgDom.style.width = this.$refs.dragImgDom.offsetWidth * 0.95 + 'px'

this.$refs.dragImgDom.style.height = this.$refs.dragImgDom.offsetHeight * 0.95 + 'px'

if (parseInt(this.$refs.dragImgDom.style.width) < 70) {

this.$refs.dragImgDom.style.width = '70px'

}

if (parseInt(this.$refs.dragImgDom.style.height) < 70) {

this.$refs.dragImgDom.style.height = '70px'

}

}

// 鼠标为中心的开始点,如果去掉这个将会以左上角开始滚动图片

this.newWidth = this.$refs.dragImgDom.offsetWidth

this.newHeight = this.$refs.dragImgDom.offsetHeight

this.$refs.dragImgDom.style.left = this.$refs.dragImgDom.offsetLeft - this.scaleX * (this.newWidth - this.oldWidth) + 'px'

this.$refs.dragImgDom.style.top = this.$refs.dragImgDom.offsetTop - this.scaleY * (this.newHeight - this.oldHeight) + 'px'

}

}

}

</script>

 

<style scoped>

.div_img{

background: url("../assets/map.jpg") center/100% 100%

}

</style>

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过Vue的事件绑定和计算属性来实现鼠标位置为中心的滑轮放大缩放功能。 首先,需要在Vue组件中绑定`wheel`事件,该事件会在鼠标滚轮滚动时触发。同时,需要定义一个计算属性来计算当前鼠标位置相对于组件的位置。 ``` <template> <div ref="container" @wheel="onWheel"> <div :style="zoomStyle"> <slot></slot> </div> </div> </template> <script> export default { data() { return { zoom: 1 } }, computed: { containerRect() { return this.$refs.container.getBoundingClientRect() }, zoomStyle() { return { transform: `scale(${this.zoom})` } } }, methods: { onWheel(event) { const delta = event.deltaY < 0 ? 0.1 : -0.1 const mouseX = event.clientX - this.containerRect.left const mouseY = event.clientY - this.containerRect.top const zoomX = (mouseX / this.containerRect.width) * delta const zoomY = (mouseY / this.containerRect.height) * delta this.zoom += zoomX + zoomY } } } </script> ``` 在`onWheel`方法中,首先根据滚动方向确定放大还是缩小,然后计算出鼠标相对于组件左上角的位置,再根据鼠标位置和滚动方向来计算出缩放比例的增量,最后更新当前的缩放比例即可。 通过计算属性`zoomStyle`将缩放比例应用到组件的样式中,使组件内容随着缩放比例的变化而放大缩小。 需要注意的是,为了让鼠标位置始终处于放大缩小的中心,需要根据鼠标位置计算出缩放比例增量时,分别计算横向和纵向的增量,再相加。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值