css特效之动态数据滚动效果
看效果:
THML代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>动态数据滚动效果</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<section>
<h2>Scroll To Reveal</h2>
</section>
<div class="container"></div>
<script>
for (let i = 0; i <= 60; i++) {
const box = document.createElement("div");
box.classList.add("box");
document.querySelector(".container").appendChild(box);
}
const randomColorBlock = document.querySelectorAll(".box");
function addColor() {
randomColorBlock.forEach((e) => {
e.style.background = randomColor();
});
}
function randomColor() {
const chars = "1234567890abcdef",
colorLength = 6;
let color = "";
for (let i = 1; i <= colorLength; i++) {
const rondomColors = Math.floor(Math.random() * chars.length);
color += chars.substring(rondomColors, rondomColors + 1);
}
return "#" + color;
}
addColor();
const boxes = document.querySelectorAll(".box");
function scrollTrigger() {
boxes.forEach((boxxx) => {
if (boxxx.offsetTop < window.scrollY) {
boxxx.classList.add("active");
} else {
boxxx.classList.remove("active");
}
});
}
window.addEventListener("scroll", scrollTrigger);
</script>
</body>
</html>
CSS代码:
@import url("https://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800,900&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
background-color: #111;
overflow-x: hidden;
}
section {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
section h2 {
position: relative;
color: #fff;
font-size: 8vw;
font-weight: 500;
}
.container {
position: relative;
top: -200px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
width: 700px;
grid-gap: 30px;
}
.container .box {
position: relative;
top: 50vh;
width: 200px;
height: 200px;
transition: 0.5s;
background-color: #fff;
border-radius: 10px;
}
.container .box:nth-child(3n + 1) {
transform: translate(-400px, 0) scale(0);
}
.container .box:nth-child(3n + 2) {
transform: translate(0, 400px) scale(0);
}
.container .box:nth-child(3n + 3) {
transform: translate(400px, 0) scale(0);
}
.container .box.active {
transform: translate(0, 0) scale(1);
}