簡體   English   中英

如何使用mousewheel插件調節功能?

[英]How to throttle function using the mousewheel plugin?

我正在使用這個插件: https : //github.com/jquery/jquery-mousewheel

這是一個水平滾動的一頁站點,該功能旨在用作“ scrollsnap”-當前部分上的滾動將強制將其捕捉到下一部分。 但是,在第一個“快照”之后,它只會在大約5秒鍾后發生,我認為這是由於函數連續觸發了太多次。

function scrollSnap() {
    $('.page:not(:last-child)').each(function(){
        var nextTarget = $(this).next().position().left;
        $(this).mousewheel(function(){
            if(event.deltaY >= 50) {
                $('main').animate({
                    scrollLeft: nextTarget
                }, 700);
                console.log("scrolled to: ", nextTarget);
            }
        });
    });

    $('.page:not(:first-child)').each(function(){
        var prevTarget = $(this).prev().position().left;
        $(this).mousewheel(function(){
            if(event.deltaY <= -50) {
                $('main').animate({
                    scrollLeft: prevTarget
                }, 700);
                console.log("scrolled to: ", prevTarget);
            }
        });
    });
}

$(document).ready(function() {
    scrollSnap();
});

您可以使用稱為反跳功能的東西來限制此類事件。 您可以創建自己的數據庫 ,也可以使用內置的lodash等實用程序庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM