jQuery animate({left:“+ = 10”})无效

时间:2011-04-19 21:58:49

标签: jquery jquery-animate

<script type="text/javascript" src="./Scripts/jquery-1.5.1.min.js"></script>
<script type='text/javascript' >
    $(document).ready(function () {
        $("#start").animate({ left: "+=10px" }, 1000)
            .animate({ left: "-5000px" }, 1000);
    });
</script>

<p id="start">this is some text that i am going to animate </p>

我在这里绞尽脑汁为什么这不起作用。如果我将left更改为marginLeft,它将按预期工作并动画。但我不确定为什么left没有。

有人可以解释一下吗?我见过许多其他脚本做类似的事情。

2 个答案:

答案 0 :(得分:10)

要使定位起作用,您需要通过设置position css属性来启用它。

将其设置为fixedabsoluterelative才能正常工作(取决于您尝试实现的目标

引用position MDC docs属性的规范。

  

初始值静态

  

<强>静态
      正常行为。 top right bottom left 属性不会   应用

答案 1 :(得分:1)

您可以将此<p>的位置设置为absolute

喜欢:p{position:absolute;}否则请使用marginLeft代替left