简体   繁体   中英

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
  });
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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM