I have a shimmer React component with the following CSS
JavaScript
x
3
1
background: #ebebeb;
2
background-image: linear-gradient(to right, #ebebeb 0%, #c5c5c5 20%, #ebebeb 40%, #ebebeb 100%);
3
and the animation keyframe I apply to it is as follows:
JavaScript
1
5
1
{
2
0%: { background-position: -468px 0 };
3
100%: { background-position: 468px 0 };
4
}
5
My home page is quite heavy on mount. So the animation freezes for about a second or so. I read that animating transition is done off-thread https://www.phpied.com/css-animations-off-the-ui-thread/
Can anyone help me do my shimmer effect in a similar off-thread manner?
Advertisement
Answer
Use transform as suggested in the link. Here is an idea with pseudo element:
JavaScript
1
22
22
1
.box {
2
background-image: linear-gradient(to right, #ebebeb calc(50% - 100px), #c5c5c5 50%, #ebebeb calc(50% + 100px));
3
background-size:0;
4
height: 200px;
5
position:relative;
6
overflow:hidden;
7
}
8
.box::before {
9
content:"";
10
position:absolute;
11
top:0;
12
right:0;
13
width:calc(200% + 200px);
14
bottom:0;
15
background-image:inherit;
16
animation:move 2s linear infinite;
17
}
18
@keyframes move{
19
to {
20
transform:translateX(calc(50% + 100px));
21
}
22
}
JavaScript
1
3
1
<div class="box">
2
3
</div>