How to use CSS, D3 and GSAP to implement horizontal bar loader (source code attached)

不言
Release: 2018-09-15 16:17:07
Original
1786 people have browsed it

The content of this article is about how to use CSS, D3 and GSAP to implement horizontal bar loader (source code attached). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.

Effect preview

How to use CSS, D3 and GSAP to implement horizontal bar loader (source code attached)

Source code download

https://github.com/comehope/front-end-daily -challenges

Code interpretation

Define dom, the container contains 1 span element:

<div>
    <span></span>
</div>
Copy after login

Centered display:

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: black;
}
Copy after login

Define the container size:

.loader {
    width: 40em;
    height: 1em;
    font-size: 10px;
}
Copy after login

Set the style of span in the container, which is a colorful slender bar:

.loader {
    position: relative;
}

.loader span {
    position: absolute;
    width: inherit;
    height: inherit;
    background-color: hsl(24, 100%, 65%);
}
Copy after login

Introduce d3.js:

<script></script>
Copy after login
Copy after login

Delete the span element in the dom and create it using d3 instead. The constant COUNT is the number of slender strips, because the data generated by d3.range() It is a sequence starting from 0, so the data is corrected to a sequence starting from 1 according to daily habits:

const COUNT = 1;

d3.select('.loader')
    .selectAll('span')
    .data(d3.range(COUNT).map(d => d + 1))
    .enter()
    .append('span')
Copy after login

Delete the span element background-color## set in the css # For the attribute code, use d3 settings instead:

d3.select('.loader')
    .selectAll('span')
    .data(d3.range(COUNT).map(d => d + 1))
    .enter()
    .append('span')
    .style('background-color', `hsl(24, 100%, 65%)`)
Copy after login
Change the number of slender bars to 2, and change the color to dynamic generation:

const HUE_DEG = 12;
const COUNT = 2;

d3.select('.loader')
    /* ...略 */
    .style('background-color', (d) => `hsl(${d * HUE_DEG}, 100%, 65%)`)
Copy after login
Go further, change the color to colored bars and black bars appear at intervals. Please note that although the value of hue in the expression is incremented by 12, the actual effect seen is that the hue difference between the colored bars is 24, because there are long black bars mixed in:

d3.select('.loader')
    /* ...略 */
    .style('background-color', (d) => d % 2 !== 0
        ? `hsl(${d * HUE_DEG}, 100%, 65%)`
        : 'black');
Copy after login
At this time, the dynamically generated dom structure is:

<p>
    <span></span>
    <span>
</span></p>
Copy after login
Introduce the gsap library:

<script></script>
Copy after login
Copy after login
Add the animation effect of the strip extending from the center to both sides:

let animation = new TimelineMax({repeat: -1});
animation.staggerFrom('.loader span', 0.5, {scaleX: 0}, 0.4)
Copy after login
Finally, change the number of strips to 30, which is obtained by dividing the 360 ​​degrees of the entire hue circle by the hue interval:

const COUNT = 360 / HUE_DEG;
Copy after login
You're done!

Related recommendations:

How to use pure CSS to implement the button effect of moving right when hovering (source code attached)

How to use CSS and GSAP to implement it Continuous animation with multiple keyframes (source code attached)

The above is the detailed content of How to use CSS, D3 and GSAP to implement horizontal bar loader (source code attached). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template