[英]Webkit Keyframes: Calculate translate amount using JavaScript or other
Take a look at this simple JsFiddle I created . 看看我创建的这个简单的JsFiddle 。
What it does is simply inserts a new li
element with a slide effect from left, when the ul
display is on flex
and inline-block
. 当
ul
显示器位于flex
和inline-block
上时,它只是从左侧插入一个带有滑动效果的新li
元素。
Something similar to that is Stackoverflow chat avatars when someone joins. 与之类似的是某人加入时的Stackoverflow聊天头像。
@-webkit-keyframes enter {
0% { /*opacity:0;*/ -webkit-transform: translateX(-100%); }
100% { /*opacity:1;*/ -webkit-transform: translateX(0px); }
}
@-webkit-keyframes moves {
0% { /*opacity:0;*/ -webkit-transform: translateX(-50px); }
100% { /*opacity:1;*/ -webkit-transform: translateX(0px); }
}
in my enter
animation, I start with translate -100%
because I want my item come from left distanced by his size. 在我的
enter
动画中,我首先要进行-100%
翻译,因为我希望我的物品从左移到他的大小。
and in the moves
animation, I want to move the whole ul
to the right, by the size of the entering element. 在
moves
动画中,我想按输入元素的大小将整个ul
向右移动。
Now I set it hard-coded, to 50px
because my elements are set to 50px
: 现在,我将其硬编码设置为
50px
因为我的元素设置为50px
:
li {
width: 50px;
height: 50px;
display: inline-block;
background-color: red;
}
How can I make it calculate the width OR height automatically, on how much to translate the ul
? 我如何才能自动计算
ul
的宽度或高度?
Example: Calculate these 50px automatically 示例: 自动计算这些50px
You can do this by animating only the added element by using negative margin: 您可以通过使用负边距仅对添加的元素进行动画处理来实现此目的:
setTimeout(() => { var item = $("<li></li>").addClass("enter"); $("ul").prepend(item).addClass('move'); }, 2000);
ul { display: flex; transition-timing-function: ease-out; transition: all .2s; } li { --h:50px; width: 50px; height: var(--h); display: inline-block; background-color: red; } .enter { animation: enter 1s; } @keyframes enter { 0% { transform: translateX(-100%); margin-left: calc(var(--h) * -1); } 100% { transform: translateX(0px); margin-left: 0 } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="my-flex-thing">a</li> <li class="my-flex-thing">b</li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.