动画图像向左和向右连续移动

jQuery: animate image to go left and right continuously

本文关键字:连续 移动 图像 动画      更新时间:2023-09-26

我试图让我的图像不断地从左到右来回移动,但它一直向左,从不返回右。我正在遵循我发现的代码,我似乎找不到我做错了什么。

http://jsfiddle.net/7kmmxeeg/1/

$(document).ready(function() {
	function firstleft() {
		$(".block.first").animate({"left": "-=100px" }, 1500, "swing", firstright);
	}
	function firstright() {
		$(".block.first").animate({"right": "+=100px" }, 1500, "swing", firstleft);
	}
	firstleft();
});
div.container {
	position:relative;	
	background-color:rgba(149, 187, 206, 0.36);
	height:700px;
	overflow:hidden;
}
div.block {
	height:200px;
	width:200px;
	background-color:blue;
	overflow:hidden;
}
div.block.first	{
	position:absolute;
	left:100px;
	top:300px;
	overflow:hidden;
}
div.block.second {
	position:absolute;
	top:10px;
	left:500px;
	background-color:green;
	overflow:hidden;
}
div.block.third {
	position:relative;
	top:350px;
	left:500px;
	background-color:red;
}
div.block.first img {
	background-size:cover;
	width:400px;
	margin-left:-100px;
}
div.block.second img {
	background-size:cover;
	width:400px;
	margin-left:-100px;
}
div.block.third img {
	background-size:cover;
	width:400px;
	margin-left:-100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
	<div class="block first">
		<img src="img/first.jpg">	
	</div>
	
	<div class="block second">
		<img src="img/second.jpg">
	</div>
	
	<div class="block third">
		<img src="img/third.jpg">
	</div>
</div>

ANSWER:将firstright()函数中的right改为left

function firstleft() {
    $(".block.first").animate({"left": "-=100px" }, 1500, "swing", firstright);
}
function firstright() {
    $(".block.first").animate({"left": "+=100px" }, 1500, "swing", firstleft);
}
<标题>更新小提琴