2014-10-30 82 views
2

我有兩個函數animation1()animation2()。這些功能中的每一個都包含一些鏈中的一些動畫(animate())。每個動畫集有兩個部分:在SCENE1jQuery mousewheel只在動畫未完成時觸發一次

  1. 移動對象>隱藏它們>場景顯示對象2

  2. 第二動畫集是在反向 - 上SCENE2隱藏對象>上SCENE1顯示對象並將它們移動到右側

動畫正在工作。

我需要的是這些動畫結合mousewheel本規則:

  • 當我向下滾動>運行動畫1
  • 時,我向上滾動>運行animation2
  • 不同時動畫1運行animation2尚未完成(反之亦然)
  • (TODO)由於鼠標輪不會運行naimation1多次(對於動畫2而言也是如此)

這是我的代碼,它正在工作,但運行多次,並創建了一個大混亂,因爲mousewheel三角洲發射多次。我嘗試了許多沒有運氣的解決方案。我也嘗試解除綁定mousewheel,這是工作;但我不知道如何重新綁定並向上滾動。

$(window).bind('DOMMouseScroll MozMousePixelScroll mousewheel onmousewheel', function(e){ 
    //$(this).unbind('DOMMouseScroll MozMousePixelScroll mousewheel onmousewheel'); 
    if (e.originalEvent.wheelDelta < 0 || e.originalEvent.detail > 0) { 
     if ($('.scene1 .image').is(':visible') && $('#content').find(":animated").length === 0) { 
     animation1(); 
     } 
    } 
    else { 
     if ($('.scene2 .image').is(':visible') && $('#content').find(":animated").length === 0) { 
     animation2(); 
     } 
    } 
}); 

感謝您的任何建議!

UPDATE 這時,這是我最好的解決辦法..動畫只發射一次(使用jquery.mousewheel,但也可以使用沒有這種LIB) http://jsfiddle.net/wkupb1Ln/6/

$('#content').mousewheel(function(event) { 
    if ($('.image').is(':animated')) { 
     return false; 
    } 
    if ((event.deltaY < 0) && $('.scene2 .image').hasClass('invisible')) { 
     animation1(); 
    } 
    if ((event.deltaY > 0) && $('.scene1 .image').hasClass('invisible')) { 
     animation2(); 
    } 
}); 

回答

1

嘗試scrollstop/scrollstart插件在這裏找到:https://github.com/ssorallen/jquery-scrollstop

然後,我會重構代碼類似於以下內容,在那裏你通過計算一定的時間間隔後的滾動來測量「德爾塔」

$(window).on("scrollstart", function() { 
    var Position1 = $window.scrollTop(); 
    setTimeout(function() { 
    var Position2 = $window.scrollTop(), delta = Position2 - Position1; 
    if (delta < 0 && $('.scene1 .image').is(':visible') && $('#content').find(":animated").length === 0) { 
     animation1(); 
    } else { 
     if (delta > 0 && $('.scene2 .image').is(':visible') && $('#content').find(":animated").length === 0) { 
     animation2(); 
     } 
    } 
    }, 250); 
}); 

綁定到「scrollstart」應確保適當的功能只觸發一次。

+0

謝謝,但暫停對我來說不是解決方案。我試過你的代碼,第二個動畫運行多次。 – 5ulo 2014-11-01 11:01:09