滚动指示器
创建于 2024-12-03 /
23
字体:
[默认]
[大]
[更大]
了解如何使用 CSS 和 JavaScript 创建滚动指示器。
如何创建滚动指示器
步骤 1) 添加 HTML:
实例
<div class="header"><h2>Scroll Indicator</h2>
<div class="progress-container">
<div class="progress-bar" id="myBar"></div>
</div>
</div>
<div>content...</div>
步骤 2) 添加 CSS:
实例
/* 设置标题样式:固定位置(始终保持在顶部)*/.header {
position: fixed;
top: 0;
z-index: 1;
width: 100%;
background-color: #f1f1f1;
}
/* 设置标题样式:固定位置(始终保持在顶部)*/
.progress-container {
width: 100%;
height: 8px;
background: #ccc;
}
/* 进度条(滚动指示器)*/
.progress-bar {
height: 8px;
background: #4caf50;
width: 0%;
}
步骤 3) 添加 JavaScript:
实例
// 当用户滚动页面时,执行 myFunctionwindow.onscroll = function() {myFunction()};
function myFunction() {
var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
var scrolled = (winScroll / height) * 100;
document.getElementById("myBar").style.width = scrolled + "%";
} 亲自试一试 »
0 人点赞过