这篇文章主要讲解了“Vue图片监听鼠标滑轮滚动怎么实现图片缩小放大功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Vue图片监听鼠标滑轮滚动怎么实现图片缩小放大功能”吧!
在js中,onmousewheel是鼠标滑轮滚动事件,可以通过这个事件触发来改变图片的大小,实现图片放大缩小功能。但是我们这里是vue所以使用的是:mousewheel。@mousewheel来监听鼠标滑轮滚动。
<div id="productDrawing">
<div class="alert">温馨提示:查看图纸时滚动鼠标可以放大缩小</div>
<div class="productDrawing_Img" @mousewheel="handerPictu">
<img
id="oImg"
src="../images/1.png"
alt=""
:
/>
</div>
</div>
然后就可以在里面编写自己的业务逻辑了。
handerPictu(e) {
const img = document.getElementById("oImg");
this.imgWidth = img.offsetWidth || img.width || img.clientWidth;
this.imgHeight = img.offsetHeight || img.height || img.clientHeight;
if (e.deltaY > 0) {
console.log("鼠标向下滚动,图片缩小");
this.imgWidth = `${this.imgWidth - 10}px`;
this.imgHeight = `${this.imgHeight - 10}px`;
} else {
console.log("鼠标向上滚动,图片放大");
this.imgWidth = `${this.imgWidth + 10}px`;
this.imgHeight = `${this.imgHeight + 10}px`;
}
// this.imgWidth = `${this.imgWidth}px`;
console.log(this.imgWidth, this.imgHeight, "hou");
},
},
当鼠标在这个图片滚动滑轮的时候就会被这个时间监听到,然后就可以写自己的逻辑代码了。
单纯的使图片缩小放大还不至于使用防抖和节流啥的,但是如果需要请求后台记得做好防抖。
可作为组件使用:
<template>
<div id="productDrawing">
<div class="alert">温馨提示:查看图纸时滚动鼠标可以放大缩小</div>
<div class="productDrawing_Img" @mousewheel="handerPictu">
<img
id="oImg"
src="../images/1.png"
alt=""
:
/>
</div>
</div>
</template>
<script>
export default {
data() {
return {
imgWidth: "auto",
imgHeight: "auto",
};
},
mounted() {},
methods: {
handerPictu(e) {
const img = document.getElementById("oImg");
console.log(img.offsetWidth, img.width, img.clientWidth);
this.imgWidth = img.offsetWidth || img.width || img.clientWidth;
this.imgHeight = img.offsetHeight || img.height || img.clientHeight;
if (e.deltaY > 0) {
console.log("鼠标向下滚动,图片缩小");
this.imgWidth = `${this.imgWidth - 10}px`;
this.imgHeight = `${this.imgHeight - 10}px`;
} else {
console.log("鼠标向上滚动,图片放大");
this.imgWidth = `${this.imgWidth + 10}px`;
this.imgHeight = `${this.imgHeight + 10}px`;
}
// this.imgWidth = `${this.imgWidth}px`;
console.log(this.imgWidth, this.imgHeight, "hou");
},
},
};
</script>
<style scoped lang="scss">
#productDrawing {
width: 580px;
height: 480px;
border: 1px solid #edf1f5;
overflow: hidden;
.alert {
height: 30px;
font-size: 12px;
line-height: 30px;
border-radius: 2px;
color: #9e7700;
text-align: center;
background: linear-gradient(90deg, #ffffff 0%, #fff7d3 50%, #fcfcfc 100%);
}
.productDrawing_Img {
width: 580px;
height: 450px;
overflow: hidden;
display: table-cell;
vertical-align: middle;
text-align: center;
img {
max-width: 100%;
max-height: 100%;
}
}
}
</style>
mousewheel
mousewheel鼠标滚轮,显而易见动动鼠标滚轮就能触发事件,但是用光标拖拽滚动条就不能触发事件。
wheelDelta、wheelDeltaX和wheelDeltaY值
这属性值是一个抽象值,表示轮子转了多远。如果滚轮旋转远离用户,则为正,否则为负。这意味着增量值符号不同于DOM级别3事件的符号车轮。但是,这些值的数量在不同浏览器之间的意义并不相同。详情见以下解释。
IE和Opera (Presto)仅支持属性和do不支持水平滚动。
这wheelDeltaX属性值指示沿水平轴的属性值。当用户操作设备向右滚动时,该值为负。否则,也就是说,如果向左,则值为正。
这wheelDeltaY属性值指示沿垂直轴的属性值。值的符号与车轮三角洲属性值。
有火狐鼠标滚轮兼容问题。
onmousewheel
onmousewheel事件:会在鼠标滚轮滚动的时候被触发,对鼠标滚轮是否滚动进行判断,但是火狐浏览器不支持这个属性。DOMMouseScroll可以为火狐浏览器绑定滚动事件,它需要通过addEventListener函数来绑定。
event.wheelDellta:可以用来获取鼠标的滚动方向,对于得到的值,只看正负,往上滚是正值,往下滚是负值。火狐浏览器不支持这个方法,需要会用event.detail来获取滚轮的滚动方向,向上是负值,向下是正值。
在页面有滚动条的时候,滚动条会随着鼠标滚轮滚动而滚动,这是浏览器的默认行为,可用return false来取消浏览器的默认行为。
有火狐鼠标滚轮兼容问题。
感谢各位的阅读,以上就是“Vue图片监听鼠标滑轮滚动怎么实现图片缩小放大功能”的内容了,经过本文的学习后,相信大家对Vue图片监听鼠标滑轮滚动怎么实现图片缩小放大功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.csdn.net/weixin_48998573/article/details/129400110