我只是想每隔 second
获取一次高度值,我在 _.throttle()
中使用 vue.js 输入了第二个参数,但不起作用:(
这是我的 vue 代码:
import underscore from 'underscore';
export default {
data() {
return {
progress: 0,
height: 0,
scroll: 0
}
},
created() {
window.addEventListener('scroll', _.throttle(this.handleScroll, 300));
},
methods: {
/**
* Handle the scroll.
*/
handleScroll() {
this.height = _.throttle(document.getElementById('tags-module').offsetTop, 300);
this.scroll = window.scrollY;
this.progress = Math.floor((this.scroll/this.height)*100);
}
}
}
在 handleScroll()
方法中,this.height
我使用了 throttle 但无法获取 height
值。如果我移除 throttle
,工作正常。
请告诉我哪里错了。
最佳答案
试试这个。
methods:{
handleScroll: _.throttle(function(){
this.scroll = window.scrollY;
this.progress = Math.floor((this.scroll/this.height)*100);
}, 300),
updateHeight: _.throttle(function(){
this.height = document.getElementById('tags-module').offsetTop;
}, 1000)
},
created(){
window.addEventListener('scroll', () => {
this.updateHeight();
this.handleScroll();
});
}
关于javascript - 无法使用下划线 throttle Vue.js 获取高度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221931/