CSS布局——导航栏悬浮滚动更改背景色

图片演示

网页上下滚动前

网页上下滚动后

html代码

    <!-- 第一大部分 -->
	<div class="bg1">
		<!-- 导航栏 -->
		<div class="nav" >
			<div class="box">
				<div class="logo">
					<img src="images/logo.png">
				</div>
				
				<div class="nav-text">
					<a href="">产品</a>
					<a href="">行业方案</a>
					<a href="">客户成功</a>
					<a href="">云市场</a>
					<a href="">服务与认证</a>
					<a href="">合作伙伴</a>
					<a href="">商城</a>
					<a href="">关于</a>
				</div>
				<div class="nav-right">
					<img src="images/serch.png" alt="">
					<a class="sousuo" href="#">登录</a>
					<a class="zhuce" href="#">注册</a>
				</div>
			</div>
		</div>
	</div>
	<!-- 第二大部分 -->
	<div class="bg2">
		
	</div>

css代码

* {
	margin: 0;
	padding: 0;
}
body{
	background-color: #eeeeee;
}

/* 大banner */
.bg1 {
	width: 100%;
	background-color: lightblue;
	height: 560px;
	background-repeat:no-repeat;
}
/* 导航栏 */
.nav {
	width: 100%;
	height: 70px;
	position: fixed;
	background-color: transparent;
}
/* 导航栏 鼠标移动到位置变更颜色 */
.nav:hover {
	background-color: #111111;
}
/* 导航栏内部组件 */
.nav .box {
	width: 1200px;
	height: 70px;
	margin: 0 auto;
	display: flex;
}
/* 导航栏-logo */
.nav .box .logo {
	height: 70%;
	width: 200px;
	margin: auto;
}
.nav .box .logo img{
	height: 100%;
	width: auto;
}
/* 导航栏-标签 */
.nav .box .nav-text {
	text-align: center;
	margin: auto;
}

.nav .box .nav-text a {
	display: inline-block;
	line-height: 70px;
	margin-right: 20px;
	font-size: 20px;
	color: #ffffff;
	font-weight: 500;
	text-decoration: none;
}

.nav .box .nav-right {
	height: 70%;
	width: 200px;
	margin: auto;
	display: flex;
}

.nav .box .nav-right img{
	width: auto;
	height: 50%;
	margin: auto;
}
.nav .box .nav-right .sousuo {
	padding: 5px 20px 5px 20px;
	background-color: #0086F2;
	font-size: 15px;
	color: #FFFFFF;
	font-weight: 500;
	margin: auto;
	text-decoration: none;
	border-radius: 5px;
}
.nav .box .nav-right .zhuce {
	padding: 5px 20px 5px 20px;
	background-color: #0086F2;
	font-size: 15px;
	color: #FFFFFF;
	font-weight: 500;
	margin: auto;
	text-decoration: none;
	border-radius: 5px;
}

/* 第二大部分 */
.bg2 {
	width: 100%;
	height: 864px;
	background-color: lightgoldenrodyellow;
}

js代码

    <script>
		function scroll() {
			var top = $(".bg1").offset().top;//获取导航栏变色的位置距顶部的高度
			var scrollTop = $(window).scrollTop();//获取当前窗口距顶部的高度
			if (scrollTop <= top) {
				$('.nav').css('background-color', 'transparent');
			} else {
				$('.nav').css('background-color', '#111111');
			}
		}
		$(window).on('scroll', function() {
			scroll()
		});
	</script>

 

  • 9
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值