Javascript jQuery切换动画(也带有图像)
我想在单击事件上切换两个单独的属性: 1) div中的图像-切换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
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);
};
});