javascript - 在 vue.js 中检测滚动
问题描述
我想在检测到滚动时在 vue.js 中触发一个函数,或者在滚动超过窗口大小的 5% 或类似的东西时更好地触发一个函数。但我无法做到正确。
这是我的代码:
<template>
<div>
<Test1 v-on:scroll.passive="handleScroll"></Test1>
</div>
</template>
<script>
import Test1 from "./Test1";
export default {
name: "HomePage",
components: {
Test1
},
methods: {
handleScroll: function() {
alert("something");
}
}
};
</script>
<style >
</style>
解决方案
您可以使用滚动事件并在需要时触发您的功能
window.addEventListener('scroll', function(e){
if(e.pageY>window.innerHeight*0.05){
console.log('over 5%'); //use your function here
}
});
确保在不再需要事件侦听器或卸载组件时删除它
推荐阅读
- postgresql - 如何使用 Criteria API 进行联合查询搜索
- apache-spark - SparkSql:保持右数据集边界的左外连接的有效方法
- java - 访问 JMH 状态
- javascript - 使用 jQuery 动态设置输入值
- spring-rabbit - 如何在使用 DirectMessageListenerContainer 连接到 rabbitMQ 时处理连接侦听器异常
- java - 文件写入程序随机退出而没有错误
- c# - 在动态表中添加/编辑评论字段值
- node.js - 如何使用 $pull 和 TypeORM 来使用 Mngodb 删除对象 insude 数组?
- mongodb - 如何仅在嵌入式数组 Mongoose 中使用 findOneAndUpdate 投影更新的值?
- scala - 更新模式表并使用它