jQuery animate({left:“+=10”}) not working

旧时模样 提交于 2019-12-02 02:46:53

问题


<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>

I am racking my brain here as to why this doesn't animate. If I change left to marginLeft it will work as expected and animates. But i am not sure why left doesn't.

Can anyone explain please? I have seen many other scripts doing something similar.


回答1:


For positioning to work, you need to have enabled it by setting the position css property.

Set it to fixed or absolute or relative to work (depending on what you try to achieve)

Quoting from the specs of the position MDC docs property.

Initial value : static

and

static
Normal behavior. The top, right, bottom, and left properties do not apply.




回答2:


You set the position of this <p> to absolute

like: p{position:absolute;} otherwise use marginLeft instead of left



来源:https://stackoverflow.com/questions/5723230/jquery-animateleft-10-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!