您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关vue中怎么监听回到顶部滚动事件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
鼠标滚到到页面中间出现的工具浮框
<template> <div class="tools"> <ul @mouseleave="mouseLeave()"> <li @click="toTop(step)">回到顶部</li> <li @mouseover="mouseOver(1)">QQ</li> <li @mouseover="mouseOver(2)">微信</li> </ul> <div v-if="showIndex === 1">玩QQ</div> <div v-if="showIndex === 2">玩微信</div> </div> </template>
<script>
export default {
name: 'FloatTools',
props: {
step: { //此数据是控制动画快慢的
type: Number,
default: 50
}
},
data() {
return {
isActive: false,
showIndex:0//默认显示下标
}
},
methods: {
toTop(i) {
//参数i表示间隔的幅度大小,以此来控制速度
document.documentElement.scrollTop -= i;
if (document.documentElement.scrollTop > 0) {
var c = setTimeout(() => this.toTop(i), 16);
} else {
clearTimeout(c);
}
},
handleScroll() {
//获取滚动距顶部的距离,显示
let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > 60) {
this.isActive = true;
} else {
this.isActive = false;
}
},
mouseOver(index) {
//鼠标移到哪个工具上,对应内容显示出来
this.showIndex = index;
},
mouseLeave(){
//鼠标移出工具区域后1秒,内容区域消失
let timer = setTimeout(() => {
this.showIndex = 0;
clearTimeout(timer)
}, 500);
}
},
mounted: function () {
window.addEventListener('scroll', this.handleScroll, true); // 监听(绑定)滚轮滚动事件
},
destroyed() {
window.removeEventListener('scroll', this.handleScroll); //离开页面需要移除这个监听的事件
}
}
</script>如果遇到scroll一直打印是0,看是否样式写了overflow:auto去掉即可;或者给父级撑满屏幕;
看完上述内容,你们对vue中怎么监听回到顶部滚动事件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。