javascript - 无法使用下划线 throttle Vue.js 获取高度值

标签 javascript underscore.js vue.js

我只是想每隔 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/

相关文章:

javascript - 使用 transclude 属性时,对象和字符串的工作方式不同

javascript - 如何使用 Jquery 触发此 CSS 动画?

javascript - 有没有优雅的函数可以在数组中查找数组? JavaScript

javascript - 使用 underscore.js 将多个数组组合为对象集合

javascript - 路由名称上的 VueJS 条件类绑定(bind)?

javascript - 在 firebase 内部调用 'this' once 函数

javascript - 元组数组的 JSON 模式

javascript - Jquery 隐藏在鼠标悬停时显示特定的 div 元素

javascript - 如何将附加变量传递给下划线模板

javascript - Vue js 结合两个组件的元素