50 天学习 50 个项目 - HTMLCSS and JavaScript
day06-scroll-animation(滚动-动画)
效果
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>scroll-animation</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<!-- 标题 -->
<h1>Scroll to see the animation-滚动查看动画</h1>
<!-- 内容-一共13个 -->
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<div class="box">
<h2>Content</h2>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
/* 在这个特定的代码中,url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap') 是字体样式表的 URL,它指定了要导入的字体是 "Roboto" ,并且包含了两个不同的字重(400 和 700)。字体的 display=swap 参数告诉浏览器,在字体加载完成之前,先使用默认的字体进行渲染,然后在字体加载完毕后再切换到所需的字体。 */
* {
box-sizing: border-box;
}
body {
background-color: #efedd6;
font-family: 'Roboto', sans-serif;
display: flex;
flex-direction: column;
/* 将主轴对齐方式设为竖直方向,以便于标题,内容居中显示 */
align-items: center;
justify-content: center;
margin: 0;
overflow-x: hidden;
}
/* 标题 */
h1 {
margin: 10px;
}
/* 内容 */
.box {
background-color: steelblue;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
width: 400px;
height: 200px;
margin: 10px;
border-radius: 10px;
box-shadow: 2px 4px 5px rgba(0, 0, 0, 0.3);
transform: translateX(400%);
/* 转换:将其向右移动自身的400%,用于隐藏 */
transition: transform 0.4s ease;
}
/* 偶数 */
.box:nth-of-type(even) {
transform: translateX(-400%);
/* 转换:将其向左移动自身的400%,用于隐藏 */
}
/* 显示内容 */
.box.show {
transform: translateX(0);
/* 不再移动 */
}
/* 内容中的标题 */
.box h2 {
font-size: 45px;
}
script.js
// 重点是felx transform transition 的应用
// 1.获取元素节点
const boxes = document.querySelectorAll('.box');//所有的内容节点
//2.窗口滚动事件
window.addEventListener('scroll', showBoxes);
// 4.页面显示,即先触发。否则,必须滚动后才会显示内容盒子
showBoxes();
// 3.滚动时,应该触发的事件
function showBoxes() {
const triggerBottom = window.innerHeight / 5 * 4;
console.log(triggerBottom);
console.log("===========");
// 这行代码计算了一个触发滚动事件的阈值。它使用了window.innerHeight属性获取浏览器窗口的可见高度,然后将其除以5,并乘以4。这样计算得到的值是窗口可见高度的五分之四。
// 对内容盒子进行添加show,其css主要是恢复盒子的移动
boxes.forEach(box => {
const boxTop = box.getBoundingClientRect().top;
// 获取元素 box 相对于视口(viewport)的顶部位置(距离视口顶部的距离)。它使用了 getBoundingClientRect() 方法来获取 box 元素的位置和尺寸信息,然后通过 .top 属性获取其顶部位置。
//getBoundingClientRect() 方法返回一个 DOMRect 对象,其中包含了元素的位置和尺寸信息,包括 top、bottom、left、right 等属性。这些属性表示元素相对于视口的位置。通过使用 .top 属性,我们可以获取元素顶部相对于视口顶部的距离。
console.log(boxTop);
if (boxTop < triggerBottom) {
box.classList.add('show')
} else {
box.classList.remove('show')
}
})
}