2016-03-01 102 views
1


我對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; 
}); 

});

回答

0

您可以使用JavaScript setTimeout函數添加如下的延遲。

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

+0

我聽說過這個函數,但我不知道如何在腳本中實現它。如果我只是在支架之間添加我的功能,它不起作用 – Wallow

0

好吧,我有一個很難實現它,因爲我不是JS好,但這裏是我做了什麼,它的作品真的很好;

$(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; 
}