1

我想在点击事件上切换两个单独的属性:

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??

                     }
});

非常感谢任何帮助!

4

2 回答 2

0

如果你稍微重构你的代码,你可以这样做。我想这就是你要找的...

$("#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' });

  }
});
于 2012-10-18T22:07:53.157 回答
0
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);
  };
});
于 2012-10-18T22:10:57.410 回答