代碼
提交代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 在這里用link標簽引入中文漸變色 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/chinese-gradient">
<style>
/* 清除默認樣式 */
* { padding: 0; margin: 0; }
/* 令html和body全屏顯示 */
html, body { height: 100% }
/* 上面的那欄 */
.top {
/* 設置為固定定位 */
position: fixed;
/* 距離上邊左邊為0 */
top: 0;
left: 0;
/* 寬度鋪滿屏幕 */
width: 100%;
/* 給個合適的高度 */
height: 80px;
/* 令其透明 */
opacity: 0;
/* 藍色背景 */
background: var(--寶石藍)
}
.main {
/* 給個合適的下邊距 */
margin-bottom: 80px;
/* 給個合適的高度 */
height: 1000px;
/* 漸變背景 */
background: var(--天藍)
}
/* 下面的那欄 */
.bottom {
/* 設置為固定定位 */
position: fixed;
/* 距離下邊左邊為0 */
bottom: 0;
left: 0;
/* 寬度鋪滿屏幕 */
width: 100%;
/* 給個合適的高度 */
height: 80px;
/* 藍色背景 */
background: var(--靛藍)
}
</style>
</head>
<body>
<div class="top"></div>
<div class="main"></div>
<div class="bottom"></div>
<script>
// 獲取固定欄
const dom = document.getElementsByClassName('top')[0]
window.addEventListener('scroll', _ => {
// 獲取偏移值
const top = document.documentElement.scrollTop
// 設置一個合適的范圍
if (top <= 150) {
// 對opacity作計算,透明度從起始到1隨偏移值而改變
const opacity = top / 150
// 令上欄的透明度變成計算后的透明度
dom.style.opacity = opacity
} else {
// 在移動一定范圍后令其完全不透明
dom.style.opacity = 1
}
})
</script>
</body>
</html>
運行結果