【发布时间】:2022-01-04 09:39:28
【问题描述】:
请帮助我是 css 新手
滚动时无法将导航栏颜色更改为红色
我认为问题出在我使用的 jquery 上,如果我错了,请纠正我
我正在使用引导程序 4
下面是我使用的 Html:
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Google Font -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,300,400,700" rel="stylesheet">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!-- Custom CSS -->
<link rel="stylesheet" href="app.css">
<title>Museum of Candy</title>
</head>
<body>
<nav id="MainNavbar" class="navbar navbar-expand-md navbar-dark py-0 fixed-top">
<a href="#" class="navbar-brand">CANDY</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link" href="#">HOME</a>
<a class="nav-link" href="#">ABOUT</a>
<a class="nav-link" href="#">TICKETS</a>
</div>
</div>
</nav>
<div id="text" class="container-fluid px-0">
<div class="row align-items-center">
<div class="col-lg-6 text-white text-center d-none d-lg-block mt-5">
<h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
<h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
<h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
<h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
<h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
<h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
<h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
</div>
<div class="col-lg-6">
<img src="imgs/hand2.png" alt="hand image" class="img-fluid">
</div>
</div>
</div>
<section class="container-fluid px-0">
<div class="row align-items-center content">
<div class="col-md-6 order-md-1 order-2">
<img src="imgs/milk.png" alt="" class="img-fluid">
</div>
<!-- text-center here aligns every element inside the div to newline and the text inside it to middle that is the reason we use text center instead of "d-flex justify-content-center" class -->
<div class="col-md-6 text-center order-md-2 order-1">
<div class="row justify-content-center">
<div class="col-10 col-lg-8 blurb mb-md-0 mb-5">
<h2 class="">MUSEUM OF CANDY</h2>
<img src="imgs/lolli_icon.png" alt="" class="d-none d-lg-inline">
<p class="lead">Lorem ipsum dolor, sit amet consectetur adipisicing elit. At enim nihil dolorum vitae magni est, temporibus deleniti vero, eligendi consequuntur quibusdam assumenda sapiente repellat.</p>
</div>
</div>
</div>
</div>
<div class="row align-items-center content">
<div class="col-md-6">
<div class="row justify-content-center">
<div class="col-10 col-lg-8 text-center blurb mb-md-0 mb-5">
<h2 class="">MUSEUM OF CANDY</h2>
<img src="imgs/lolli_icon.png" alt="" class="d-none d-lg-inline">
<p class="lead">Lorem ipsum dolor, sit amet consectetur adipisicing elit. At enim nihil dolorum vitae magni est, temporibus deleniti vero, eligendi consequuntur quibusdam assumenda sapiente repellat.</p>
</div>
</div>
</div>
<!-- text-center here aligns every element inside the div to newline and the text inside it to middle that is the reason we use text center instead of "d-flex justify-content-center" class -->
<div class="col-md-6 text-center">
<img src="imgs/gumball.png" alt="" class="img-fluid">
</div>
</div>
<div class="row align-items-center content">
<div class="col-md-6 order-md-1 order-2">
<img src="imgs/sprinkles.png" alt="" class="img-fluid">
</div>
<!-- text-center here aligns every element inside the div to newline and the text inside it to middle that is the reason we use text center instead of "d-flex justify-content-center" class -->
<div class="col-md-6 text-center order-md-2 order-1">
<div class="row justify-content-center">
<div class="col-10 col-lg-8 blurb mb-md-0 mb-5">
<h2 class="">MUSEUM OF CANDY</h2>
<img src="imgs/lolli_icon.png" alt="" class="d-none d-lg-inline">
<p class="lead">Lorem ipsum dolor, sit amet consectetur adipisicing elit. At enim nihil dolorum vitae magni est, temporibus deleniti vero, eligendi consequuntur quibusdam assumenda sapiente repellat.</p>
</div>
</div>
</div>
</div>
</section>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script>
$(function () {
$(document).scroll(function () {
var $nav = $("#mainNavbar");
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
});
});
</script>
</body>
</html>
我使用了一些我自己的 css
而我的css如下:
body{
background-color: #f5d9d5;
font-family: "Nunito";
}
#MainNavbar{
font-size: 1.5rem;
font-weight: 100;
}
#MainNavbar .navbar-brand{
color: #ea1c2c;
font-size: 1.5rem;
}
#MainNavbar .nav-link{
color: white;
}
#MainNavbar .nav-link:hover{
color: #ea1c2c;
}
#text .slash{
color: #ea1c2c;
}
h1{
font-weight: 100;
font-size: 4rem;
}
.blurb h2{
color: #ea1c2c;
font-weight: 100;
font-size: 2.5rem;
}
.blurb p{
font-weight: 100;
font-size: 1.125rem;
line-height: 2;
color: #f498b8;
}
.content{
margin-top: 100px;
margin-bottom: 100px;
}
.navbar.scrolled {
background-color: red
}
@media (max-width:1200px){
h1{
font-weight: 100;
font-size: 3rem;
}
.blurb h2{
font-size:2rem;
}
}
点击下面的链接查看我的项目文件夹结构
【问题讨论】:
-
您的选择器引用似乎不正确,应该是
#MainNavbar而不是#mainNavbar。如果这仍然不起作用,请尝试确定问题是您的样式还是在指定条件下应用类的脚本 - 您可以通过在滚动事件侦听器函数中记录 (console.log($nav.hasClass('scrolled'))) 来做到这一点。每次满足条件时都应该注销。 -
非常感谢它已经解决了我从昨天开始再次遇到这个问题感谢您抽出时间帮助我????
标签: html jquery css flexbox css-selectors