Web Front-end
CSS Tutorial
How to use CSS, D3 and GSAP to implement horizontal bar loader (source code attached)How to use CSS, D3 and GSAP to implement horizontal bar loader (source code attached)
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

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>
Centered display:
body {
margin: 0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background-color: black;
}
Define the container size:
.loader {
width: 40em;
height: 1em;
font-size: 10px;
}
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%);
}
Introduce d3.js:
<script></script>
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')
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%)`) 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%)`) 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'); At this time, the dynamically generated dom structure is:
<p> <span></span> <span> </span></p>Introduce the gsap library:
<script></script>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)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;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!
Where should 'Subscribe to Podcast' link to?Apr 16, 2025 pm 12:04 PMFor a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:
Browser Engine DiversityApr 16, 2025 pm 12:02 PMWe lost Opera when they went Chrome in 2013. Same deal with Edge when it also went Chrome earlier this year. Mike Taylor called these changes a "Decreasingly
UX Considerations for Web SharingApr 16, 2025 am 11:59 AMFrom trashy clickbait sites to the most august of publications, share buttons have long been ubiquitous across the web. And yet it is arguable that these
Weekly Platform News: Apple Deploys Web Components, Progressive HTML Rendering, Self-Hosting Critical ResourcesApr 16, 2025 am 11:55 AMIn this week's roundup, Apple gets into web components, how Instagram is insta-loading scripts, and some food for thought for self-hosting critical resources.
Git Pathspecs and How to Use ThemApr 16, 2025 am 11:53 AMWhen I was looking through the documentation of git commands, I noticed that many of them had an option for . I initially thought that this was just a
A Color Picker for Product ImagesApr 16, 2025 am 11:49 AMSounds kind of like a hard problem doesn't it? We often don't have product shots in thousands of colors, such that we can flip out the with . Nor do we
A Dark Mode Toggle with React and ThemeProviderApr 16, 2025 am 11:46 AMI like when websites have a dark mode option. Dark mode makes web pages easier for me to read and helps my eyes feel more relaxed. Many websites, including
Some Hands-On with the HTML Dialog ElementApr 16, 2025 am 11:33 AMThis is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.






