How can I animate the opposite way?

一个人想着一个人 提交于 2019-12-22 10:35:04

问题


Greetings,

I am changing the width of an element which serves as a bar , and that works. However I can't make it so that it animates it in the opposite direction. I tried putting - in front of bar_width but to no avail. Width will be dynamically calculated it's just that I want the direction to go to the left rather than to the right like so <------- not ----------->

var bar_width=$(this).css('width') ;
$(this).css('width', '0').animate({ width: bar_width }, queue:false,duration:3500,easing: easing});

回答1:


You could animate it from right to left by animating both the margin-left and width of the element at the same time:

CSS

#bar {
    margin-left: 100px;
    height: 10px;
    width: 0;
    background: red;
}

jQuery

$('#bar').animate({
    marginLeft: 0,
    width: 100
});

In action here.

Alternative, if your element is positioned absolutely you could animate the left property and width at the same time.




回答2:


Instead of 0 you can use "toggle" as the parameter, like this:

$(this).animate({ width: "toggle" }, queue:false, duration:3500, easing:easing});

"toggle" will animate from it's full width to 0, and the reverse next time.



来源:https://stackoverflow.com/questions/3552765/how-can-i-animate-the-opposite-way

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