Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery切换动画(也带有图像)_Javascript_Jquery_Animation_Onclick_Toggle - Fatal编程技术网

Javascript jQuery切换动画(也带有图像)

Javascript jQuery切换动画(也带有图像),javascript,jquery,animation,onclick,toggle,Javascript,Jquery,Animation,Onclick,Toggle,我想在单击事件上切换两个单独的属性: 1) div中的图像-切换src属性 2) div的绝对位置-切换动画(“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="emaildo

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

1) div中的图像-切换
src
属性

2) div的绝对位置-切换
动画(“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>

非常感谢任何帮助

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

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

  }
});
另外,请查看以下内容:

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

伟大的。。。这是做切换动画,谢谢。。。但是,图像src切换现在不起作用,因为它已经将您的代码放在下面了!我被一些旧代码篡改了。解决了的!非常感谢,非常感谢。。。。我盯着它看了很久了!我很高兴能帮助你,同时我也很开心:)
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);
  };
});