2010-04-23 37 views
0

我想在jQuery中獲得一個簡單的效果,只有幾個小時的經驗,所以有很多東西需要學習。如何在使用jQuery的用戶事件之前和之後爲css屬性創建動畫?

我設法得到這個代碼的工作:

<script type="text/javascript" > 
    $(document).ready(function() { 
     lastBlock = $("#a1"); 
     maxWidth = 415; 
     minWidth = 75; 

     $("ul li a").hover(function() { 
      $(lastBlock).animate({ width: minWidth + "px" }, { 
      queue: false, 
      duration: 600 
      }); 
      $(this).animate({ width: maxWidth + "px" }, { 
      queue: false, 
      duration: 600 
      }); 
      lastBlock = this; 
     }); 

    }); 
</script> 

這給了我正是我想要的,一個6窗格水平手風琴效果。然而,每個窗格的左上角都有一個75x75的圖像,無論哪個窗格處於活動狀態(並且這個圖像在懸停時都會導致窗格打開)時始終可見。

我想要做的是讓選定的'窗格'上的圖像將頂部邊距降到10px,然後在選擇新的時候將其放回0px,即所選的窗格圖像始終比其他5張圖片低10px。

我懷疑這應該很容易,但還不完全掌握語法。

謝謝。

回答

1

如果我理解你,它應該是一個簡單的問題鏈接與動畫方法的回調。

<script type="text/javascript" > 
    $(document).ready(function() { 
     lastBlock = $("#a1"); 
     maxWidth = 415; 
     minWidth = 75; 

     $("ul li a").hover(function() { 
      $(lastBlock).animate({ width: minWidth + "px" }, { 
      queue: false, 
      duration: 600 
      }, function() { 
       $(this).find('img').animate({ topMargin: "-=10" }); // remove 10px 
      }); 
      $(this).animate({ width: maxWidth + "px" }, { 
      queue: false, 
      duration: 600 
      }, function() { 
       $(this).find('img').animate({ topMargin: '+=10' }); // add 10px 
      }); 
      lastBlock = this; 
     }); 

    }); 
</script> 
+0

似乎它應該工作,但它不...原始行爲仍然適用於此代碼,但圖像仍然存在...我錯過了什麼? – user318573 2010-04-23 18:53:03

+0

您可能想嘗試添加填充而不是頂部邊距。如果ul已經超過它上面的元素10px以上,則邊距可能已經被處理了。 – tvanfosson 2010-04-23 19:00:04

相關問題