如何将参数传递给称为另一个函数参数的函数?

时间:2018-09-30 12:12:01

标签: javascript

让我解释一下我要做什么。我有一个控制滚动链接动画的功能。我想在requestAnimationFrame()中调用它,但是我想将window.pageYOffset作为函数的参数传递。现在,代码如下所示:

let wH = window.offsetHeight;
let ticking = false
function effects(wT, wH){
//animations using wT, wH
};

window.onresize = () => {
    if (!ticking) {
        wH = window.offsetHeight;
        const wT = window.pageYOffset;
        //I want to pass the wT, and wH to the rAF's parameter,
        //which is the function above. How?
        requestAnimationFrame(effects);
        ticking = true;
    }
}
window.onscroll = () => {
    const wT = window.pageYOffset;
    if (!ticking) {
        //I want to pass the wT, and wH to the rAF's parameter,
        //which is the function above. How?
        requestAnimationFrame(effects);
        ticking = true;
    }
}

1 个答案:

答案 0 :(得分:2)

将该函数调用包装在匿名函数中,如下所示:

requestAnimationFrame(function() {
    effects(wT, wH);
});

使用箭头功能缩小:

requestAnimationFrame(() => effects(wT, wH));