簡體   English   中英

jQuery切換動畫(也帶有圖像)

[英]jQuery toggling an animation (with an image too)

我想在click事件上切換兩個單獨的屬性:

1)div中的圖像-切換src屬性

2)div的絕對位置-切換animate("top":"0px")

這是HTML

<div id="emailme">
   <a id="email" href="mailto:xxx@gmail.com">xxx@gmail.com</a>
   <div id="emailmecopy">email me</div>
   <img id="emaildown"src="images/downbutton.png">
</div>

現在,我已經對src切換進行了排序,但是無法弄清楚如何切換動畫位:

$("#emailme img").on({
      'click': function() {
        var src = ($(this).attr('src') === 'images/downbutton.png')
              ? 'images/upbutton.png'
              : 'images/downbutton.png';
            $(this).attr('src', src);   //THIS TOGGLES THE IMAGE SRC

            //BUT WHAT DO I PUT HERE TO TOGGLE ANIMATE BETWEEN ("top":"0px") and 
            //("top":"-50px") IN THE SAME CLICK??

                     }
});

任何幫助,不勝感激!

如果您稍微重構代碼,可以這樣做。 我想這就是你想要的...

$("#emailme img").on({
  'click': function() {

    var $this = $(this);
    var src = $this.attr('src');
    var isUp = src === 'images/upbutton.png';
    var newSrc = 'images/'+ (isUp ? 'downbutton' : 'upbutton') +'.png';

    $this.attr('src', newSrc)
      .animate({ top: (isUp ? '-50' : '0') +'px' });

  }
});
if($("#emailme img").css('top') == '0px'){
  $("#emailme img").stop().animate({top:'-50px'},1000);
};
if($("#emailme img").css('top') == '-50px'){
  $("#emailme img").stop().animate({top:'0px'},1000);
};

還檢查一下:

$('body').on('click', '#emailme img', function (e){
  $(this).attr('src', $(this).attr('src')=='images/downbutton.png'?'images/upbutton.png':'images/downbutton.png');
  if($(this).css('top') == '0px'){
    $(this).stop().animate({top:'-50px'},1000);
  };
  if($(this).css('top') == '-50px'){
    $(this).stop().animate({top:'0pg'},1000);
  };
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM