CSS3 keyframe progress bar

孤人 提交于 2020-01-03 03:31:06

问题


When you see the fiddle you will see that the animation is somehow jumpy. It should got from left to right to 100% width and then squeeze back to 0% width on the right side. How to fix that?

http://jsfiddle.net/j44gbwna/

div:after {
    content: "";        
    height: 3px;
    background: red;        
    position: absolute;
    animation: loader 2s;
    -webkit-animation: loader 2s;
    animation-iteration-count: infinite;
    -webkit-animation-iteration-count: infinite;
    transition-timing-function: linear;
    -webkit-transition-timing-function: linear;
    bottom: 0px;        
    margin-left: 0;
}

@keyframes loader {
    0% {left: 0px;}
    99% { left: 100%;} 
}

@-webkit-keyframes loader {
    0% {width: 0%;left:0;}
    50% { width: 100%;} 
    99% { width: 0%;right:0;} 
}

回答1:


When doing CSS keyframe animations, try to define as many properties as you can in all the steps, because when you introduce a new property mid-animation, it usually takes the default/initial value which is sometimes different from what you'd expect:

@-webkit-keyframes loader {
    0% {width: 0%;left:0;right:0}
    50% { width: 100%;left:0;right:0} 
    99% { width: 0%;left:100%;right:0} 
}

http://jsfiddle.net/j44gbwna/3/

div:after {
	content: "";
	/* width: 40px; */
    height: 3px;
    background: red;
    
	position: absolute;
    animation: loader 2s;
    -webkit-animation: loader 2s;
    animation-iteration-count: infinite;
    -webkit-animation-iteration-count: infinite;
    transition-timing-function: linear;
    -webkit-transition-timing-function: linear;
	bottom: 0px;
    
	margin-left: 0;
}

@keyframes loader {
    0% {width: 0%;left:0;right:0}
    50% { width: 100%;left:0;right:0} 
    99% { width: 0%;left:100%;right:0} 
}

@-webkit-keyframes loader {
    0% {width: 0%;left:0;right:0}
    50% { width: 100%;left:0;right:0} 
    99% { width: 0%;left:100%;right:0} 
}
<div></div>



回答2:


I came up with this one while playing around. Not exactly what you were looking for (which is in the second code snippet), but it produces a neat little effect.

@-webkit-keyframes loader {
    0% {width: 0%;right: 100%;}
    50% { width: 100%;} 
    99% { width: 0%; right:0;} 
}

div:after {
	content: "";
	/* width: 40px; */
    height: 3px;
    background: red;
    
	position: absolute;
    animation: loader 2s;
    -webkit-animation: loader 2s;
    animation-iteration-count: infinite;
    -webkit-animation-iteration-count: infinite;
    transition-timing-function: linear;
    -webkit-transition-timing-function: linear;
	bottom: 0px;
    
	margin-left: 0;
}

@keyframes loader {
    0% {left: 0px;}
    99% { left: 100%;} 
}

@-webkit-keyframes loader {
    0% {width: 0%;right: 100%;}
    50% { width: 100%;} 
    99% { width: 0%; right:0;} 
}
<div></div>

It has to do with the fact that you are trying to animate a property (right) for which you defined no start value (and the way left and right values are treated on position: absolute elements). It get's complicated. The easiest thing to do is to always have the loader at 100% width and just use transforms to display it correctly:

div { 
    overflow:hidden;
    position: absolute;
    bottom:0; left:0; right:0;
    height: 4px;
}

div:after {
	content: "";
	/* width: 40px; */
    height: 3px;
    background: red;
    
	position: absolute;
    animation: loader 2s;
    -webkit-animation: loader 2s;
    animation-iteration-count: infinite;
    -webkit-animation-iteration-count: infinite;
    transition-timing-function: linear;
    -webkit-transition-timing-function: linear;
	bottom: 0px;
    left:0; width:100%;
	margin-left: 0;
}

@-webkit-keyframes loader {
    0% {transform: translateX(-100%);}
    100% {transform: translateX(100%);} 
}
<div></div>


来源:https://stackoverflow.com/questions/29416077/css3-keyframe-progress-bar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!