[英]Animating a div's width with transform property
Basically, I want my div to show from left to right by altering it's width.基本上,我希望我的 div 通过改变它的宽度从左到右显示。 But when
transform: translate(-50%,-50%);
但是
transform: translate(-50%,-50%);
is present, the div's width animates from it's center to the assigned position.存在时,div 的宽度从它的中心到指定位置进行动画处理。
I can't really change the position
to anything but absolute
since the div is placed in a specific spot.由于 div 位于特定
position
,因此我无法真正将position
更改为absolute
位置。
.Container{ z-index: 100; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); height: 150px; animation: Animate 3s forwards; } .Container:after{ content: ''; position: absolute; top: 0; left: 90%; width: 200px; height: 150px; background-color: rgb(110, 110, 110); transform: skew(-35deg); transform-origin: 100% 0; z-index: 99; } @keyframes Animate{ from{ width: 0px; } to{ width: 250px; background-color: red; } }
<div class="Container"> <div class="projDescription"> <h2>Hi</h2> <p>Hello</p> <br> <a href="#">Help?</a> </div> </div>
Would it be better if I used Jquery instead?如果我改用 Jquery 会更好吗?
Also, I'll ask this here too since it has to do with the same div;另外,我也会在这里问这个问题,因为它与相同的 div 有关; can I animate the
:after
pseudo element?我可以为
:after
伪元素设置动画吗? It's mostly just there to give the div an angled style, and I know I could've just used something else to style it.它主要是为了给 div 一个有角度的风格,我知道我可以用别的东西来设计它。 But I was just curious if it is possible.
但我只是好奇是否有可能。
Just remove the transform
property from your .Container
class and use calc()
if you know the exact value of the width
and height
如果您知道
width
和height
的确切值,只需从.Container
类中删除transform
属性并使用calc()
.Container{
z-index: 100;
position: absolute;
top: calc(50% - 75px);
left: calc(50% - 125px);
height: 150px;
animation: Animate 3s forwards;
}
.Container:after{
content:'';
position: absolute;
top: 0;
left: 90%;
width: 200px;
height: 150px;
background-color: rgb(110, 110, 110);
transform: skew(-35deg);
transform-origin: 100% 0;
z-index: 99;
}
@keyframes Animate{
from{
width: 0px;
}
to{
width: 250px;
background-color: red;
}
}
I'd move some attributes to inner classes and made small modifications to achieve it;我会将一些属性移动到内部类并进行一些小的修改来实现它; and also I used
scale
for width expansion animation instead of animation width
property, to avoid an unnecessary reflow并且我还使用
scale
用于宽度扩展动画而不是动画width
属性,以避免不必要的回流
.Container{ z-index: 100; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); width: 250px; height: 150px; } .projDescription{ height: 100%; transform: scaleX(0); position: absolute; top: 0; width:100% ; animation: Animate 3s forwards; } .Container:after{ content: ''; position: absolute; top: 0; left: 90%; width: 200px; height: 100%; box-sizing: border-box; background-color: rgb(110, 110, 110); transform: skew(-35deg) translateX(-50%); transform-origin: 100% 0; z-index: 99; animation: move 3s forwards; } @keyframes Animate{ to{ transform: scaleX(1); background-color: red; } } @keyframes move{ to{ transform: skew(-35deg) translateX(0%); } }
<div class="Container"> <div class="projDescription"> <h2>Hi</h2> <p>Hello</p> <a href="#">Help?</a> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.