禁用临时滚动功能

时间:2016-03-01 23:29:34

标签: javascript jquery scroll

我是JS的新手。我有一个脚本检测哈希并根据滚动的方向改变它(基本上:向下滚动=#1,#2,#3 ......等)。但我想在每个滚动之间或每次散列更改之间添加延迟。我无法弄清楚如何做到这一点。
这是我的代码:

$(function (){
$(window).on('wheel', function(e) {
    var hash = window.location.hash.substring(2);
    var length = $("section").length;
    var delta = e.originalEvent.deltaY;

    if (delta > 0 && hash == length)
        window.location.hash = "#s" + 1;
    else if (delta < 0 &&  hash == 1)
        window.location.hash = "#s" + length;
    else {
        if (delta > 0) {
            hash++;
            window.location.hash = "#s" + hash;
        }
        else {
            hash--;
                window.location.hash = "#s" + hash;
        }
    }
    return false;
});

});

2 个答案:

答案 0 :(得分:0)

您可以使用JavaScript setTimeout函数添加延迟,如下所示。

setTimeout(function(){ your code; }, 3000);

答案 1 :(得分:0)

Okay, I had a hard time implementing it since I'm not good in JS, but here's what I've done, it works really well;

$(window).on('wheel', scroll);
function scroll(e) {
    var hash = window.location.hash.substring(2);
    var length = $("section").length;
    var delta = e.originalEvent.deltaY;

    if (delta > 0 && hash == length)
        window.location.hash = "#s" + 1;
    else if (delta < 0 && hash == 1)
        window.location.hash = "#s" + length;
    else {
        if (delta > 0) {
            hash++;
            window.location.hash = "#s" + hash;
        }
        else {
            hash--;
            window.location.hash = "#s" + hash;
        }
    }
    $(window).off('wheel', scroll);
    setTimeout(function(){
        $(window).on('wheel', scroll);
    }, 1000);
    return false;
}