Css Animations - Animation Is Slow And Jiggles
Solution 1:
Background size is a visual property and so any change to its value would cause repainting to occur. Painting is a very expensive operation and is bound to have an impact on the performance in low end machines. One way to overcome this would be to use CSS transform
(scale to be precise) instead of background-size
change to produce the animation.
Snippet which will cause performance impact:
The below snippet uses the same animation as in the question. When you run this snippet and inspect it using Chrome Dev tools (by enabling "Show Paint Rects" option), you'd see that both images have a paint rect associated with them (green or red colored box) and that as the animation is happening the box keeps blinking (or stays as-is). This indicates that a repaint is happening often and thus it impacts performance.
.anim-on,
.anim-out {
height: 200px;
width: 200px;
background: url(http://lorempixel.com/200/200/nature/1);
}
.anim-on {
background-size: 110%110%;
background-position: center center;
animation: shrink 12s infinite alternate;
}
.anim-out {
background-size: 120%120%;
background-position: center center;
animation: small 6s infinite alternate;
}
@keyframes shrink {
0% {
background-size: 110%110%;
}
100% {
background-size: 100%100%;
}
}
@keyframes small {
0% {
background-size: 100%100%;
}
100% {
background-size: 110%110%;
}
}
/* Just for demo */div {
float: left;
margin-right: 20px;
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script><divclass='anim-on'></div><divclass='anim-out'></div>
Snippet which will cause lesser performance impact:
In the below snippet, I have added the background-image
to a pseudo-element and then used scale
transform on it to produce the zoom-in/out effect. The parent's overflow: hidden
setting prevents the animation from affecting its size. If you inspect this with Chrome Dev tools you'd see that the green or red colored box appears only once when the page is loaded and goes away. This indicates that there is no further repaint is happening during the animation itself and hence it is better from a performance point of view. You'd also notice that this animation is more smoother than the earlier one.
.anim-on,
.anim-out {
position: relative;
height: 200px;
width: 200px;
overflow: hidden;
}
.anim-on:after,
.anim-out:after {
position: absolute;
content: '';
height: 100%;
width: 100%;
top: 0px;
left: 0px;
background: url(http://lorempixel.com/200/200/nature/1);
}
.anim-on:after {
animation: shrink 12s infinite alternate;
}
.anim-out:after {
animation: small 6s infinite alternate;
}
@keyframes shrink {
0% {
transform: scale(1.1);
}
100% {
transform: scale(1);
}
}
@keyframes small {
0% {
transform: scale(1);
}
100% {
transform: scale(1.1);
}
}
/* Just for demo */div {
float: left;
margin-right: 20px;
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script><divclass='anim-on'></div><divclass='anim-out'></div>
You can find more information about the various CSS properties and how a change to their value will impact the rendering process in the CSS Triggers website.
You can find more information about the rendering process and how using transform
(as opposed to few other properties) results in a performance improvement in the below articles/sites:
Solution 2:
I edited Henry code, now css is reusable, so user can add background image to element via CMS, and css code will do the rest:
.anim {
position:relative;height:100vh;width:100%;overflow:hidden;**background-size:0px!important;**
}
.anim:after {
position:absolute;content:'';height:100%;width:100%;top:0px;left:0px;background-size:cover!important;**background:inherit;**z-index:-1;
}
.anim:after {
animation:shrink12sinfinitealternate;
}
@keyframesshrink {
0% {
transform:scale(1.1);
}
100% {
transform:scale(1);
}
}
@keyframessmall {
0% {
transform:scale(1);
}
100% {
transform:scale(1.1);
}
}
<sectionclass="anim"style="background:url('images/1.png');"></section>
Thanks * :)
Post a Comment for "Css Animations - Animation Is Slow And Jiggles"