我试图动画的一组图像,分别移动300像素的权利,在第一次点击和再次点击移动300像素左回到起始位置。
我现在所拥有的不是工作。所以这要么是A)我的语法有问题,要么是B)我的图像在5px时没有真正呈现出来。
编辑:这个小提琴现在更新了正确的代码
Jsfiddle Demo
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<style type="text/css">
#wrapper {
width: 80em;
}
.image {
width: 10em;
height: 10em;
display: block;
position: relative;
left:5px;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$(".image").click(function() {
if ($('this').css('left') =='5px') {
$(".image").animate({"right": "300px"},3000);
else
$(".image").animate({"left": "300px"},3000);
}
});
});
</script>
</head>
<body>
<div id="wrapper">
<section id="gallery">
<img id="avery" class="image" src='images/OAI.jpg' alt= "On Avery Island Album Art">
<img id="overthesea" class="image" src='images/ITAOTS.jpg' alt="In the Aeroplane Over the Sea album art">
<img id="beauty" class="image" src='images/beauty.jpg' alt="Beauty Demo Album Art">
<img id="everthingIs" class="image" src='images/everythingis.jpg' alt="Eveything Is EP Album Art">
</section>
</div>
</body>
</html>
最佳答案
试试这个:
$(document).ready(function () {
$(".image").click(function () {
var th = $(this);
if ($(th).css('left') == '5px') {
console.log("ret");
$(th).animate({
"left": "300px"
}, 3000);
} else {
console.log("sdffsdsff");
$(th).animate({
"left": "5px"
}, 3000);
}
});
});
Fiddle here.
关于javascript - jQuery Animate Image在单击事件时右移,然后在第二单击事件时返回左,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19763771/