简体   繁体   English

左侧框的动画宽度

[英]animate width of a box towards left

I have a div box 我有一个div盒子

<div style="width: 400px; height: 400px; position: absolute; left: 350px; top: 350px; background-color: #cdcdcd;"></div>

i can easily animate its width towards right side (left edge will be on fixed position). 我可以轻松地向右侧设置其宽度的动画(左边缘将位于固定位置)。
How to fix the right edge of the box and animate its width towards left??? 如何固定盒子的右边缘并将其宽度向左移动???
ie box will expand towards left side. 即盒子将向左侧扩展。

You can do it by animating the box's margin-left and width at the same time. 您可以通过同时为框的边距和宽度设置动画来实现。 Alternatively, if your box is absolutely positioned, animate it's left property and width at the same time. 或者,如果您的盒子是绝对定位的,则同时为其左侧属性和宽度设置动画。

HTML HTML

<div id="loading"></div>

CSS CSS

#loading {
   margin-left: 200px;
   width: 0;
   height: 10px;
   background: red;
}

jQuery jQuery的

$('#loading').animate({width: 200, marginLeft: 0}, {duration: 1000});

You can see it in action here . 你可以在这里看到它。

If I'm reading the question right, then you anyways have position absolute applied so rather than using left property use right property to position the box and then animate the width? 如果我正在读正确的问题,那么你无论如何都应该使用绝对位置,而不是使用左边属性使用右边的属性来定位框然后设置宽度的动画?

http://jsfiddle.net/nakYV/115/ something like this. http://jsfiddle.net/nakYV/115/这样的事情。

<div class="thisIsContainer"> <div id="thisToAnimate"></div></div>

.thisIsContainer { width: 300px; display: flex; justify-content: flex-end; }
#thisToAnimate { width: 200px; transition linear width 0.3s;} 

and lets say You would like an animation on hover then: 并且让我们说你想要悬停的动画然后:

#thisToAnimate:hover { width: 300px; }

Due to justify-content: flex-end our 'box' will stick to right, then on hover it will expand to left. 由于对齐内容:flex-end我们的'box'将向右移动,然后在悬停时它会向左扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM