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

Callback inside of an event listener for window is not working

本文关键字:回调 不起作用 内部 事件 侦听器 窗口      更新时间:2023-09-26
(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);

当我尝试运行这个时,它根本不起作用。我一直得到未定义不是函数,为什么呢?它是否与它在eventListener内部有关?还是我在函数内部传递了参数?这太疯狂了,我已经用过很多次了,只是现在似乎在eventListener中有问题。

有谁能在这个问题上给我一些启发吗?

现实示例

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

去掉变量

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

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

根据你的意见,我认为你不需要打电话了

cb.call(this,scrll);

只做

cb.(scrll);