2016-02-29 59 views
1

我有一些應該在用戶滾動後出現的div。所以我跟蹤mouseScroll事件。但我想爲我的活動增加一些延遲。我試圖使用setTimeout,但它並沒有幫助...如果我做了一個完整的滾動滾動(每個行爲多於一個),除了刪除我的課程.shown之外,我什麼也得不到。我該怎麼辦?這裏是我的代碼:向DOMMouseScroll事件添加延遲

$(document).ready(function() { 
 
    var timer; 
 
    var delay = 1000; 
 

 
    $(window).bind('mousewheel DOMMouseScroll', function(event) { 
 
    var shown = $(".shown").removeClass('shown'); 
 
    timer = setTimeout(function() { 
 
     if (event.originalEvent.wheelDelta > 14 || event.originalEvent.detail < 14) { 
 
     // scroll up 
 
     if (shown.next() && shown.next().length) { 
 
      shown.next().addClass('shown'); 
 
     } else { 
 
      shown.siblings(":first").addClass('shown'); 
 
     } 
 
     } else { 
 
     // scroll down 
 
     if (shown.next() && shown.next().length) { 
 
      shown.next().addClass('shown'); 
 

 
     } else { 
 
      shown.siblings(":first").addClass('shown'); 
 
     } 
 
     } 
 
    }, delay); 
 
    }, function() { 
 
    clearTimeout(timer); 
 
    }); 
 
});
.view { 
 
    width: 300px; 
 
    height: 20px; 
 
    display: none; 
 
    clear: both; 
 
    transition: opacity 1s ease-out; 
 
    opacity: 0; 
 
} 
 
#first { 
 
    background: grey; 
 
} 
 
#second { 
 
    background: red; 
 
} 
 
#third { 
 
    background: blue; 
 
} 
 
#fourth { 
 
    background: orange; 
 
} 
 
#fifth { 
 
    background: green; 
 
} 
 
.view.shown { 
 
    display: block; 
 
    opacity: 1; 
 
    -webkit-animation: fadeIn 1s; 
 
    animation: fadeIn 1s; 
 
} 
 
@-webkit-keyframes fadeIn { 
 
    from { 
 
    opacity: 0; 
 
    } 
 
    to { 
 
    opacity: 1; 
 
    } 
 
} 
 
@keyframes fadeIn { 
 
    from { 
 
    opacity: 0; 
 
    } 
 
    to { 
 
    opacity: 1; 
 
    } 
 
}
<div class="view shown" id="first"></div> 
 
<div class="view" id="second"></div> 
 
<div class="view" id="third"></div> 
 
<div class="view" id="fourth"></div> 
 
<div class="view" id="fifth"></div>

不幸的是,我不知道,也,如何扭轉這個腳本相同的延遲。任何幫助將不勝感激!

回答

2

嘗試使用:

$(window).bind('mousewheel DOMMouseScroll', function(event) { 
    var shown = $(".shown").removeClass('shown'); 
    clearTimeout(timer); 
    timer = setTimeout(function() { 
     if (event.originalEvent.wheelDelta > 14 || event.originalEvent.detail < 14) { 
     // scroll up 
     if (shown.next() && shown.next().length) { 
      shown.next().addClass('shown'); 
     } else { 
      shown.siblings(":first").addClass('shown'); 
     } 
     } else { 
     // scroll down 
     if (shown.next() && shown.next().length) { 
      shown.next().addClass('shown'); 

     } else { 
      shown.siblings(":first").addClass('shown'); 
     } 
     } 
    }, delay); 
    }); 
+0

它的工作原理,但並不像預期 - 如果你做一個「大」長卷,系統craches。我需要更改「大」滾​​動...我該怎麼做錯了?... – Varg

+0

這是什麼意思「大」滾動?請顯示代碼上下文。這可能是崩潰的另一個原因,不是因爲滾動。 –

+0

系統跟蹤事件「滾動」,但當我做另一個時,它刪除類「顯示」,沒有增加。這段代碼有助於一對一滾動,真的有用。但爲了改善它,我必須做很多事情。 [Fulpage.js](https://www.google.com.ua/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwj-yf_E9Z3LAhWrC5oKHc6kCBcQFggaMAA&url=http%3A%2F%2Falvarotrigo。 com%2FfullPage%2F&usg = AFQjCNETe6ZqOIznFqASVcshwcCM10-Tmg&sig2 = Rn_My6Km7U-v1vdrE1WfPQ&bvm = bv.115339255,d.bGs)解決了我的問題,我想說「謝謝」您的幫助! – Varg