我怎样才能以相反的方式制作动画?

发布于 2024-09-15 20:41:58 字数 310 浏览 3 评论 0原文

您好,

我正在更改用作 bar 的元素的宽度,并且有效。然而我不能 使它朝相反的方向动画。我尝试将 - 放在 bar_width 前面,但无济于事。宽度将动态计算,只是我希望方向向左而不是向右,就像这样 <-------- 不是 ---------->

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

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});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[旋木] 2024-09-22 20:41:58

您可以通过同时设置元素的 margin-leftwidth 动画来从右向左设置动画:

CSS

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

jQuery

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

在此处执行

或者,如果您的元素是绝对定位的,您可以为 left 属性和 width 同时

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.

紫南 2024-09-22 20:41:58

您可以使用 "toggle" 作为参数,而不是 0,如下所示:

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

"toggle" 将从其全宽度动画到 0,下次则相反。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文