Search code examples
javascriptcallapplyiife

Callback inside of an event listener for window is not working


(function(w,d,u){
    var k = function(cb){
        window.addEventListener('scroll',function(cb){
            var scrll = this.scrollY;
            if(cb){ 
                cb.call(this,scrll);
            }
        },false);
    };
    return (window.scrollex=k);
})(this,document);

When I try running this it does not work at all. I keep getting undefined is not a function, why is this? Does it have to do with something of it being inside of the eventListener? Or that I passed the argument inside of the function? It's crazy I've used this many of times just seem to be having problems with it now inside of an eventListener.

Can anyone enlighten me on this issue?

Real life Example

scrollr('element',{
  max:500,
  min:400,
  swing:50,
  name:"custom_event",
  dir:"left",
  trigger:"mousemove || scroll"
 },function(t,a,c){
    //t returns an object
    //a returns the current position of the trigger if it is a mousemove or scroll
    //c returns a boolean, which is if the element is inView of the viewport
  });

Solution

  • window.addEventListener('scroll',function(cb){  <--cb is an event object
    

    get rid of the variable

    window.addEventListener('scroll',function(){
    

    ============

    Based on your comments, I do not think you need call

    cb.call(this,scrll);
    

    just do

    cb.(scrll);