如何使我的导航栏从无阴影变为阴影,并在滚动条上进行淡入淡出过渡?
How do I make my navbar go from no shadow to shadow with fade transition on scroll?
我希望我的导航栏在滚动时从无阴影变为阴影 — 完成(阅读下面我的回答)。现在我想为滚动上的阴影添加淡入淡出过渡效果。阅读下面的“更新”。
$(document).ready(function() {
// Transition effect for navbar
$(window).scroll(function() {
// checks if window is scrolled more than 500px, adds/removes solid class
if($(this).scrollTop() > 500) {
$('.topnav').addClass('shadow');
} else {
$('.topnav').removeClass('shadow');
}
});
});
.logo {
float: left;
padding-left: 20px;
padding-right: 15px;
padding-bottom: 30px;
padding-top: 20px;
width: 210px;
height: auto;
}
.topnav {
overflow: hidden;
background-color: white;
top: 0;
left: 0;
margin: 0;
padding: 0;
z-index: 1000;
width: 100%; !important;
position: fixed;
}
.topnav.shadow {
-webkit-box-shadow: 0px 0px 9px 3px rgba(41,41,41,.25);
-moz-box-shadow: 0px 0px 9px 3px rgba(41,41,41,.25);
box-shadow: 0px 0px 9px 3px rgba(41,41,41,.25);
}
.topnav a {
float: left;
display: block;
color: grey;
text-align: center;
padding-top: 30px;
padding-bottom: 0px;
padding-left: 16px;
padding-right: 16px;
text-decoration: none;
font-size: 18px;
transition: color 0.3s ease-out;
}
.active, .nav-btn {
color: #1a1a1a;
}
@media (max-width: 800px) {
.resp-t-n {
padding-top: 60px;
}
.resp-b-n {
padding-bottom: 30px;
}
.logo {
width: 130px;
height: auto;
}
}
.topnav .icon {
display: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: grey;
padding: 34px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
transition: color 0.3s ease-out;
}
.dropdown-content {
display: none;
position: fixed;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1001;
}
.dropdown-content a {
float: none;
color: grey;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav a:hover:not(.logo), .dropdown:hover .dropbtn {
color: #1a1a1a;
transition: color 0.5s ease;
}
.dropdown-content a:hover {
background-color: #ddd;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
@media screen and (max-width: 800px) {
.topnav a:not(:first-child), .dropdown .dropbtn .resp-t-n .resp-b-n {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 800px) {
.topnav.responsive {
position: fixed;
}
.topnav.responsive .icon {
position: fixed;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
.topnav.responsive .dropdown {float: none;}
.topnav.responsive .dropdown-content {position: relative;}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
}
#active {
color: #1a1a1a;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="topnav" id="myTopnav">
<img class="logo" src="[img path]">
<a href="/" id="active"><div class="resp-t-n">Home</div></a>
<a href="elements.html">Elements</a>
<a href="about.html">About</a>
<a href="contact.html"><div class="resp-b-n">Contact</div></a>
<a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()"><i class="fal fa-bars"></i></a>
</div>
我正在尝试学习如何从头开始编写代码以及任何我不知道的东西,我通常只是搜索,但我真的找不到有效的解决方案,因此我的第一个 post .
非常感谢任何帮助:)
在我的控制台中发现一个错误:
未捕获的引用错误:$ 未定义
在 main.js:14
第14行的代码是
$(document).ready(function() {
— 我解决了这个问题
已更新
我现在想为滚动上的阴影添加淡入淡出过渡。 CSS 可以实现还是必须使用 JS 进行备份?
$(document).ready(function() {
// Transition effect for navbar
$(window).scroll(function() {
// checks if window is scrolled more than 500px, adds/removes solid class
if ($(this).scrollTop() > 50) {
$('.topnav').addClass('shadow');
} else {
$('.topnav').removeClass('shadow');
}
});
});
body {
height: 200vh/* just for checking purpose */
}
.logo {
float: left;
padding-left: 20px;
padding-right: 15px;
padding-bottom: 30px;
padding-top: 20px;
width: 210px;
height: auto;
}
.topnav {
overflow: hidden;
background-color: white;
top: 0;
left: 0;
margin: 0;
padding: 0;
z-index: 1000;
width: 100%;
position: fixed;
}
.topnav.shadow {
-webkit-box-shadow: 0px 0px 9px 3px rgba(41, 41, 41, .25);
-moz-box-shadow: 0px 0px 9px 3px rgba(41, 41, 41, .25);
box-shadow: 0px 0px 9px 3px rgba(41, 41, 41, .25);
}
.topnav a {
float: left;
display: block;
color: grey;
text-align: center;
padding-top: 30px;
padding-bottom: 0px;
padding-left: 16px;
padding-right: 16px;
text-decoration: none;
font-size: 18px;
transition: color 0.3s ease-out;
}
.active,
.nav-btn {
color: #1a1a1a;
}
@media (max-width: 800px) {
.resp-t-n {
padding-top: 60px;
}
.resp-b-n {
padding-bottom: 30px;
}
.logo {
width: 130px;
height: auto;
}
}
.topnav .icon {
display: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: grey;
padding: 34px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
transition: color 0.3s ease-out;
}
.dropdown-content {
display: none;
position: fixed;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1001;
}
.dropdown-content a {
float: none;
color: grey;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav a:hover:not(.logo),
.dropdown:hover .dropbtn {
color: #1a1a1a;
transition: color 0.5s ease;
}
.dropdown-content a:hover {
background-color: #ddd;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
@media screen and (max-width: 800px) {
.topnav a:not(:first-child),
.dropdown .dropbtn .resp-t-n .resp-b-n {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 800px) {
.topnav.responsive {
position: fixed;
}
.topnav.responsive .icon {
position: fixed;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
.topnav.responsive .dropdown {
float: none;
}
.topnav.responsive .dropdown-content {
position: relative;
}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
}
#active {
color: #1a1a1a;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="topnav" id="myTopnav">
<img class="logo" src="[img path]">
<a href="/" id="active">
<div class="resp-t-n">Home</div>
</a>
<a href="elements.html">Elements</a>
<a href="about.html">About</a>
<a href="contact.html">
<div class="resp-b-n">Contact</div>
</a>
<a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()"><i class="fal fa-bars"></i></a>
</div>
您的页面 window 内容较少。如果您的 js 中提到的内容超过 500px,它会起作用。
问题是 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
在我的主 .js 文件下面。我只是把它移到上面。这么简单又愚蠢的错误!
对于我问题的第二部分——过渡——我添加了 transition: 1s;
到 .topnav
非常感谢大家的宝贵时间:)
我希望我的导航栏在滚动时从无阴影变为阴影 — 完成(阅读下面我的回答)。现在我想为滚动上的阴影添加淡入淡出过渡效果。阅读下面的“更新”。
$(document).ready(function() {
// Transition effect for navbar
$(window).scroll(function() {
// checks if window is scrolled more than 500px, adds/removes solid class
if($(this).scrollTop() > 500) {
$('.topnav').addClass('shadow');
} else {
$('.topnav').removeClass('shadow');
}
});
});
.logo {
float: left;
padding-left: 20px;
padding-right: 15px;
padding-bottom: 30px;
padding-top: 20px;
width: 210px;
height: auto;
}
.topnav {
overflow: hidden;
background-color: white;
top: 0;
left: 0;
margin: 0;
padding: 0;
z-index: 1000;
width: 100%; !important;
position: fixed;
}
.topnav.shadow {
-webkit-box-shadow: 0px 0px 9px 3px rgba(41,41,41,.25);
-moz-box-shadow: 0px 0px 9px 3px rgba(41,41,41,.25);
box-shadow: 0px 0px 9px 3px rgba(41,41,41,.25);
}
.topnav a {
float: left;
display: block;
color: grey;
text-align: center;
padding-top: 30px;
padding-bottom: 0px;
padding-left: 16px;
padding-right: 16px;
text-decoration: none;
font-size: 18px;
transition: color 0.3s ease-out;
}
.active, .nav-btn {
color: #1a1a1a;
}
@media (max-width: 800px) {
.resp-t-n {
padding-top: 60px;
}
.resp-b-n {
padding-bottom: 30px;
}
.logo {
width: 130px;
height: auto;
}
}
.topnav .icon {
display: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: grey;
padding: 34px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
transition: color 0.3s ease-out;
}
.dropdown-content {
display: none;
position: fixed;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1001;
}
.dropdown-content a {
float: none;
color: grey;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav a:hover:not(.logo), .dropdown:hover .dropbtn {
color: #1a1a1a;
transition: color 0.5s ease;
}
.dropdown-content a:hover {
background-color: #ddd;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
@media screen and (max-width: 800px) {
.topnav a:not(:first-child), .dropdown .dropbtn .resp-t-n .resp-b-n {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 800px) {
.topnav.responsive {
position: fixed;
}
.topnav.responsive .icon {
position: fixed;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
.topnav.responsive .dropdown {float: none;}
.topnav.responsive .dropdown-content {position: relative;}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
}
#active {
color: #1a1a1a;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="topnav" id="myTopnav">
<img class="logo" src="[img path]">
<a href="/" id="active"><div class="resp-t-n">Home</div></a>
<a href="elements.html">Elements</a>
<a href="about.html">About</a>
<a href="contact.html"><div class="resp-b-n">Contact</div></a>
<a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()"><i class="fal fa-bars"></i></a>
</div>
我正在尝试学习如何从头开始编写代码以及任何我不知道的东西,我通常只是搜索,但我真的找不到有效的解决方案,因此我的第一个 post .
非常感谢任何帮助:)
在我的控制台中发现一个错误:
未捕获的引用错误:$ 未定义 在 main.js:14
第14行的代码是
$(document).ready(function() {
— 我解决了这个问题
已更新
我现在想为滚动上的阴影添加淡入淡出过渡。 CSS 可以实现还是必须使用 JS 进行备份?
$(document).ready(function() {
// Transition effect for navbar
$(window).scroll(function() {
// checks if window is scrolled more than 500px, adds/removes solid class
if ($(this).scrollTop() > 50) {
$('.topnav').addClass('shadow');
} else {
$('.topnav').removeClass('shadow');
}
});
});
body {
height: 200vh/* just for checking purpose */
}
.logo {
float: left;
padding-left: 20px;
padding-right: 15px;
padding-bottom: 30px;
padding-top: 20px;
width: 210px;
height: auto;
}
.topnav {
overflow: hidden;
background-color: white;
top: 0;
left: 0;
margin: 0;
padding: 0;
z-index: 1000;
width: 100%;
position: fixed;
}
.topnav.shadow {
-webkit-box-shadow: 0px 0px 9px 3px rgba(41, 41, 41, .25);
-moz-box-shadow: 0px 0px 9px 3px rgba(41, 41, 41, .25);
box-shadow: 0px 0px 9px 3px rgba(41, 41, 41, .25);
}
.topnav a {
float: left;
display: block;
color: grey;
text-align: center;
padding-top: 30px;
padding-bottom: 0px;
padding-left: 16px;
padding-right: 16px;
text-decoration: none;
font-size: 18px;
transition: color 0.3s ease-out;
}
.active,
.nav-btn {
color: #1a1a1a;
}
@media (max-width: 800px) {
.resp-t-n {
padding-top: 60px;
}
.resp-b-n {
padding-bottom: 30px;
}
.logo {
width: 130px;
height: auto;
}
}
.topnav .icon {
display: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: grey;
padding: 34px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
transition: color 0.3s ease-out;
}
.dropdown-content {
display: none;
position: fixed;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1001;
}
.dropdown-content a {
float: none;
color: grey;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav a:hover:not(.logo),
.dropdown:hover .dropbtn {
color: #1a1a1a;
transition: color 0.5s ease;
}
.dropdown-content a:hover {
background-color: #ddd;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
@media screen and (max-width: 800px) {
.topnav a:not(:first-child),
.dropdown .dropbtn .resp-t-n .resp-b-n {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 800px) {
.topnav.responsive {
position: fixed;
}
.topnav.responsive .icon {
position: fixed;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
.topnav.responsive .dropdown {
float: none;
}
.topnav.responsive .dropdown-content {
position: relative;
}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
}
#active {
color: #1a1a1a;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="topnav" id="myTopnav">
<img class="logo" src="[img path]">
<a href="/" id="active">
<div class="resp-t-n">Home</div>
</a>
<a href="elements.html">Elements</a>
<a href="about.html">About</a>
<a href="contact.html">
<div class="resp-b-n">Contact</div>
</a>
<a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()"><i class="fal fa-bars"></i></a>
</div>
您的页面 window 内容较少。如果您的 js 中提到的内容超过 500px,它会起作用。
问题是 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
在我的主 .js 文件下面。我只是把它移到上面。这么简单又愚蠢的错误!
对于我问题的第二部分——过渡——我添加了 transition: 1s;
到 .topnav
非常感谢大家的宝贵时间:)