CSS+js:顶部导航栏背景滚动渐变、顶部背景滚动渐变

一、效果图

在这里插入图片描述
图1

在这里插入图片描述
图2

在这里插入图片描述
图3

二、gradual.html代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>顶部导航栏渐变和顶部背景渐变</title>
	</head>
	<body>
		<div class="content-root" id="contentRoot" onscroll="handleScroll()">
			<div class="content-top">
				<div class="tool-bar">
					<img class="back-img" src="../images/icon_title_back_black.png" />
					<span class="title">title样式</span>
					<img class="search" src="../images/icon_search.png" />
				</div>
				<div style="position: absolute; margin-top: 60px;">这里可以显示banner图</div>
			</div>
			<div style="height: 800px; background-color: aquamarine; position: relative;"></div>

		</div>
	</body>
	<style type="text/css">
		body {
			position: absolute;
			width: 100%;
			height: 100%;
			padding: 0;
			margin: 0;
			top: 0;
			left: 0;
			bottom: 0;
			background-color #eee;
			/* overflow: auto;
			overflow-y: scroll; */

			/* ::-webkit-scrollbar {
				display: none;
			} */
		}

		.content-root {
			width: 100%;
			height: 100%;
			top: 0;
			left: 0;
			/* 开启 onscroll */
			overflow-y: scroll;
			position: absolute;
			padding-bottom: 2.5rem;
			background-color: #7ddcf8;
		}

		.content-top {
			width: 100%;
			height: 9.375rem;
			/* 如果需要让下面的内容覆盖在这上面,使用  position: absolute; */
			position: relative;
			background-color: #dddddd;
			padding-bottom: 4.125rem;
			align-items: center;
		}


		.tool-bar {
			width: 100%;
			height: 3.125rem;
			display: flex;
			/* 固定位置 */
			position: fixed;
			background-color: transparent;
			padding-left: 1rem;
			padding-bottom: 0.9375rem;
			/* 数值越大,表示越会显示在其他堆叠元素之上 */
			z-index: 999;
			align-items: flex-end;
			justify-content: space-between;
			box-sizing: border-box;
		}
		
		.back-img {
			width: 1.875rem;
			height: 1.25rem;
			margin-left: 0.16rem;
		}

		.title {
			position: absolute;
			font-family: PingFangSC, PingFang SC;
			font-size: 1.25rem;
			left: 50%;
			transform: translate(-50%, 0);
			text-align: center;
		}

		.search {
			position: absolute;
			right: 1rem;
			width: 1.25rem;
			height: 1.25rem;
		}
	</style>
	<script>
		const contentTop = document.querySelector('.content-top')
		const toolBar = document.querySelector('.tool-bar')
		// 注意 div里 让 onscroll 调用handleScroll前提,content-root样式里添加了 overflow-y: scroll;
		function handleScroll() {
			let scrollTop = event.target.scrollTop;
			console.log('handleScroll scrollTop = ', scrollTop);
			 // 设置背景颜色的透明度
			if (scrollTop >= 100) {
				toolBar.style.backgroundColor = "#ffffff";
				 contentTop.style.backgroundColor = "#000000";
			} else if (scrollTop <= 0) {
				toolBar.style.backgroundColor = "transparent";
				contentTop.style.backgroundColor = "#dddddd";
			} else {
				toolBar.style.backgroundColor = `rgba(255, 255, 255,${scrollTop / (scrollTop + 40)})`
				contentTop.style.backgroundColor = `rgba(221,221,221,${scrollTop})`
			}
		}
	</script>
</html>

三、testH5源码

点击查看testh5源码

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的实现纵向滚动导航栏并且带有渐变效果的HTML、CSSJS代码示例: HTML代码: ```html <div id="navbar"> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> <li><a href="#section4">Section 4</a></li> <li><a href="#section5">Section 5</a></li> </ul> </div> <div id="section1"> <h2>Section 1</h2> <p>Content goes here...</p> </div> <div id="section2"> <h2>Section 2</h2> <p>Content goes here...</p> </div> <div id="section3"> <h2>Section 3</h2> <p>Content goes here...</p> </div> <div id="section4"> <h2>Section 4</h2> <p>Content goes here...</p> </div> <div id="section5"> <h2>Section 5</h2> <p>Content goes here...</p> </div> ``` CSS代码: ```css #navbar { position: fixed; top: 0; left: 0; width: 100%; height: 50px; background-color: rgba(0, 0, 0, 0.8); z-index: 999; } #navbar ul { display: flex; justify-content: center; align-items: center; height: 100%; margin: 0; padding: 0; list-style: none; } #navbar li { margin: 0 20px; } #navbar a { color: #fff; text-decoration: none; font-size: 18px; font-weight: bold; transition: color 0.2s ease-out; } #navbar a:hover { color: #f00; } .active { color: #f00 !important; } #section1 { height: 500px; background-color: #eaeaea; } #section2 { height: 500px; background-color: #dcdcdc; } #section3 { height: 500px; background-color: #cfcfcf; } #section4 { height: 500px; background-color: #bfbfbf; } #section5 { height: 500px; background-color: #afafaf; } ``` JS代码: ```javascript window.addEventListener("scroll", function() { let navbar = document.getElementById("navbar"); let sections = document.querySelectorAll("section"); let currentSectionIndex = 0; let currentSection = sections[currentSectionIndex]; for (let i = 1; i < sections.length; i++) { if (window.pageYOffset >= sections[i].offsetTop - navbar.offsetHeight) { currentSectionIndex = i; currentSection = sections[i]; } } let links = document.querySelectorAll("#navbar a"); links.forEach(link => link.classList.remove("active")); let activeLink = document.querySelector(`#navbar a[href="#${currentSection.id}"]`); activeLink.classList.add("active"); let percent = (window.pageYOffset - currentSection.offsetTop + navbar.offsetHeight) / (currentSection.offsetHeight - navbar.offsetHeight); let rgba = `rgba(0, 0, 0, ${percent * 0.8})`; navbar.style.backgroundColor = rgba; }); ``` 代码解释: - HTML代码中包含一个固定在页面顶部导航栏和一些带有ID的部分。 - CSS代码中设置了导航栏的样式以及每个部分的背景颜色。 - JS代码中为窗口滚动事件添加了监听器,用于检测当前滚动到哪个部分,并且更新导航栏的样式。在滚动过程中,导航栏背景色会渐变,从透明到不透明。渐变的程度取决于滚动到当前部分的百分比。同时,导航栏中当前部分的链接会被高亮显示。 希望这个示例能够帮助到你实现纵向滚动导航栏并且带有渐变效果的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值