Check if user is scrolling with trackpad?

后端 未结 5 1068
鱼传尺愫
鱼传尺愫 2020-12-08 23:16

I have created a site where users are able to scroll through a gallery using mouse scroll or arrow up/down. It is working like I want it to, and changing one image pr scroll

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 23:46

    This is pretty much what DC_ suggested. Figured I'd post this since it's an actual implementation.

    // similar to _.debounce, but was having issues with it, so I made this.
    function rateLimit(func, time){
            var callback = func,
                    waiting = false,
                    context = this;
            var rtn = function(){
                if(waiting) return;
                waiting = true;
                var args = arguments;
                setTimeout(function(){
                    waiting = false;
                    callback.apply(context, args);
                }, time);
            };
            return rtn;
        }
    
        function onWheel(e){
          // Add your code here
        }
    
        // will only fire a maximum of 10 times a second
        var debouncedOnWheel = rateLimit(onWheel, 100);
        window.addEventListener("wheel", debouncedOnWheel);
    

提交回复
热议问题