jQuery切换动画

时间:2009-05-31 02:25:25

标签: jquery animation toggle

我有这个jQuery:

$(document).ready(function()
{
   $("#panel").hide();

   $('.login').toggle(
   function()
   {
      $('#panel').animate({
      height: "150", 
      padding:"20px 0",
      backgroundColor:'#000000',
      opacity:.8
}, 500);
   },
   function()
   {
      $('#panel').animate({
      height: "0", 
      padding:"0px 0",
      opacity:.2
      }, 500);
   });
});

这工作正常,但我需要扩展一点功能。我也希望同样操作另一个div的属性与#panel div同步。我尝试添加两个与辅助div相关的功能,但我只是进行了4阶段切换......哈哈! 原谅我的无知!

谢谢你们!

4 个答案:

答案 0 :(得分:50)

$('.login').toggle(
    function(){
        $('#panel').animate({
            height: "150", 
            padding:"20px 0",
            backgroundColor:'#000000',
            opacity:.8
        }, 500);
        $('#otherdiv').animate({
            //otherdiv properties here
        }, 500);
    },
    function(){
        $('#panel').animate({
            height: "0", 
            padding:"0px 0",
            opacity:.2
        }, 500);     
        $('#otherdiv').animate({
            //otherdiv properties here
        }, 500);
});

答案 1 :(得分:6)

我不认为在切换功能中添加双重功能适用于已注册的点击事件(除非我遗漏了某些内容)

例如:

$('.btnName').click(function() {
 top.$('#panel').toggle(function() {
   $(this).animate({ 
     // style change
   }, 500);
   },
   function() {
   $(this).animate({ 
     // style change back
   }, 500);
 });

答案 2 :(得分:0)

使用 jquery 中的 toggle() 或 slideToggle:

Toggle

  $( "#clickme" ).click(function() {
      $( "#book" ).slideToggle( "slow", function() {
        // Animation complete.
      });
    });


 <div id="clickme">
      Click here
    </div>
    <img id="book" src="book.png" alt="" width="100" height="123">

答案 3 :(得分:-8)

onmouseover="$('.play-detail').stop().animate({'height': '84px'},'300');" 

onmouseout="$('.play-detail').stop().animate({'height': '44px'},'300');"

只需两个站点 - 一个onmouseover和一个onmouseout。