简体   繁体   English

窗口的事件侦听器内部的回调不起作用

[英]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? 它是否与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. 我已经使用了很多次了,这简直太疯狂了,现在似乎在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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