2009-05-03 37 views
0

我想有某種反彈影響我的動畫插件,但它無法正常工作。回調不叫可言:的jQuery:需要一些幫助.. .animate的回調函數不工作

$(function() { 
    var offset = $("#first").offset(); 
    var position = $(window).scrollTop(); 
    $(window).scroll(function() { 
     var scroll = $(window).scrollTop(); 
     if (scroll>position) { 
     $("body") 
      .animate({ 
      scrollTop: offset.top + $("#first").innerHeight() + 150 
      }, 
      1000, 
      "easeInOutQuart", 
      function() { 
      $("body").animate({ 
       scrollTop: offset.top - 150 
      }, 
      1000, 
      "easeOutBounce" 
      ) 
      }) 
     } 
    }) 
    }) 

好吧..這是我的HTML代碼..我不知道爲什麼你的是偉大的工作。但我的不是。該$(「HTML」)ISN 「T工作,但是你的工作很好..

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
<title>Bounce Test Pad</title> 
<link rel=stylesheet href="index.css" type= "text/css" /> 
<script type="text/javascript" src ="jquery-1.3.2.js"></script> 
<script type = "text/javascript" src="jquery.easing.1.3.js"></script> 
</head> 
<body> 
    <img id="lightbulb" src = "img/veioza.png"> 
    <div id="wrapper"> 
     <img id="first" class="images" src="img/nike.jpg" /> 
     <img id ="second" class = "images" src="img/golden.jpg" /> 
     <img id = "third" class ="images" src ="img/a.jpg" /> 
    </div> 
<script type="text/javascript"> 
    $(window).resize(function() { 
     centerIt(); 
    }); 
    $(function() { 
     centerIt(); 
    }) 
    function centerIt() { 
     var viewportWidthSize = window.innerWidth; 
     var pixels = (viewportWidthSize/2) - $("#first").width()/2; 
     $("#wrapper img").css("left", pixels); 
    }; 


    $(function() { 
     var offset = $("#first").offset(); 
     var prevpos = $(window).scrollTop(); 
     var animating = false; 
     $(window).scroll(function() { 
      var curpos = $(window).scrollTop(); 
      if (curpos>prevpos && !animating) { 
       $('html').animate(
         {scrollTop: offset.top + $("#first").height()}, 
         1000, 
         "easeInOutQuart" 
        ) 
      } 
      animating = true; 
     }) 
    }) 
</script> 
</body> 
</html> 
+0

嘗試驗證它不會通過把警報或執行console.log在那裏叫。 – 2009-05-03 10:41:39

+0

嗨Pim, 我試着在回調函數中放置一個警報。該警報被稱爲不間斷。它只是彈出警報不停止 – 2009-05-03 10:48:00

回答

3

AnhTu是正確的關於動畫提高滾動事件。

這裏有一個固定的演示:http://jsbin.com/alede(您可以編輯在這裏演示:http://jsbin.com/alede/edit

您必須添加代碼,以防止再動畫,而動畫仍在發生:

var status = $('#status'); 
var offset = $("#downcontent").offset(); 
var height = $("#downcontent").height(); 
var animating = false; 
var prevpos = $(window).scrollTop(); 

$(window).scroll(function(){ 
    var curpos = $(window).scrollTop(); 
    if (curpos > prevpos && !animating) { 
    $('html').animate(
     {scrollTop: offset.top + height}, 
     1000, 
     "easeInOutQuart", 
     function(){ 
     $('html').animate(
      {scrollTop: offset.top}, 
      1000, 
      "easeOutBounce", 
      function(){ 
      animating = false; 
      status.html('Both animations finished.<br />'); 
      } 
     ); 
     status.html('First animation finished.<br />Second animation started<br />'); 
     } 
    ); 
    animating = true; 
    status.html('First animation started<br />'); 
    } 
    prevpos = curpos; 
}); 

編輯

好啦,我已經創造了另一個演示用HTML代碼。我固定的JavaScript一點,並增加了一些CSS規則:http://jsbin.com/oqapa

0

做到這一點,而無需使用回調

if (scroll>position) { 
    $("body") 
     .animate({ 
     scrollTop: offset.top + $("#first").innerHeight() + 150 
     },1000,"easeInOutQuart") 
     .animate({ 
     scrollTop: offset.top - 150 
     },1000,"easeOutBounce") 
    }) 
} 

但我想你哈瓦綁定到其他事件。因爲當你身體的動畫,可能引發該事件window.scroll