gpt4 book ai didi

javascript - 有没有更好的方法在没有 Javascript 的情况下实现这个 CSS 动画?

转载 作者:行者123 更新时间:2023-11-29 18:07:24 26 4
gpt4 key购买 nike

只是尝试加载栏。主要问题是,一旦每个柱都完成了迭代,它就需要在最后一个柱的顶部重新开始。我通过一些 hacky JS 做到了这一点,但也许有更聪明的方法来使用纯 CSS 来做到这一点?

HTML

<div class="loading-bar">
<div class="bar"><div class="bar-inner"></div></div>
<div class="bar"><div class="bar-inner"></div></div>
</div>

SCSS

.loading-bar {
clear: both;
height: 12px;
width: 100%;

position: relative;

.bar {
margin: 0 auto;
height: 12px;
position: absolute;
top: 0;
left: 0;
width: 100%;
text-align: center;
z-index: 0;

.bar-inner {
margin: 0 auto;
width: 0;
height: 12px;
-webkit-animation-name: loadbars;
-webkit-animation-duration: 1s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-in-out;
}

&:nth-child(1) .bar-inner {
background: gray;
}
&:nth-child(2) .bar-inner {
background: blue;
-webkit-animation-delay: 0.5s;
}
}
}

@-webkit-keyframes loadbars {
0% { width: 0; }

50% { width: 100%; }

100% { width: 100%; }
}

JS

var bars = $(".bar-inner");
var zindex = 1;
var listener = function () {
zindex++;
$(this).parent().css("z-index", zindex);
};

bars.on("webkitAnimationIteration", listener);

演示: http://jsfiddle.net/18uqrtxo/

已更新

我已经更新了演示以显示带有 4 个条的纯 CSS,只是为了好玩。

http://jsfiddle.net/18uqrtxo/2/

最佳答案

z-index 设置动画。

Demo

z-index 应用于条形图,而不是它们的容器。这将允许栏在变宽时位于顶部,而在全宽时位于下方。

关于javascript - 有没有更好的方法在没有 Javascript 的情况下实现这个 CSS 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30281279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com