我想在Vue组件中收听窗口滚动事件.以下是我到目前为止所做的try :
<my-component v-on:scroll="scrollFunction">
...
</my-component>
在我的组件方法中定义了scrollFunction(event)
,但它似乎不起作用.
有人知道怎么做吗?
谢谢
我想在Vue组件中收听窗口滚动事件.以下是我到目前为止所做的try :
<my-component v-on:scroll="scrollFunction">
...
</my-component>
在我的组件方法中定义了scrollFunction(event)
,但它似乎不起作用.
有人知道怎么做吗?
谢谢
事实上我找到了解决办法.我在创建组件时在scroll
事件上添加一个事件侦听器,在销毁组件时删除事件侦听器.
export default {
created () {
window.addEventListener('scroll', this.handleScroll);
},
destroyed () {
window.removeEventListener('scroll', this.handleScroll);
},
methods: {
handleScroll (event) {
// Any code to be executed when the window is scrolled
}
}
}
希望这有帮助!