这是我的CSS:
.animate-rectangle {
background-color:#0F0;
border: 1px solid #FFF;
float:left;
position:absolute;
width:100px;
height:100px;
}和jQuery代码:
<!--Jquery Code to Animate Rectangle from Left to Right-->
$(document).ready(function () {
var windo_width = $(document).width() - 300;
$("#reset").click(function () {
$(".animate-rectangle").animate({
"left": "100px"
}, "slow");
});
$("#MoveRight").click(function () {
$(".animate-rectangle").animate({
"left": "+=" + windo_width
}, "slow");
});
});看看jQuery代码,它声明了animate({"left":"100px"},但实际上它是正确的,为什么会发生这种情况?
发布于 2013-11-15 16:59:59
在查看css时,最初并没有为元素分配left。因此,默认情况下,它会将其left作为0。所以现在你正在尝试从左边移到100,显然你的元素将按照从0 to 100移动的顺序向右移动。尝试为元素设置初始左位置。
*** ------> ***
*** ***
| |
| |
left : 0 left: 100发布于 2013-11-15 17:00:39
首先,.animate-rectangle的left位置将是0,或者是它最接近的父级的left值。动画将其向右移动,因为100的值大于它的原点。如果要将其相对于起始位置向右移动,请使用+=100px
$("#reset").click(function () {
$(".animate-rectangle").animate({
"left": "+=100px"
}, "slow");
});https://stackoverflow.com/questions/19997036
复制相似问题