0

Let me explain what I'm trying to do. I have a function which controls scroll linked animations. I want to call it inside requestAnimationFrame(), but I want to pass the window.pageYOffset as the function's parameter. Now the code looks like this:

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;
    }
}
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Gergő Horváth
  • 3,195
  • 4
  • 28
  • 64

1 Answers1

2

Wrap that function call inside an anonymous function like so:

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

Shorter using an arrow function:

requestAnimationFrame(() => effects(wT, wH));
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73