我最近开始了一个基于我为我的投资组合网站创建的虚拟公司的项目。我在实现一段jQuery代码时遇到问题,以切换一个名为hidden的类,该类将设置该类所放置的显示为none。我正在使用此类在某些媒体查询断点处切换移动导航。但是隐藏的toggleClass不起作用。你们中的任何人都可以帮忙吗? 这是我的网站: https://jorgeg1105.github.io/JG-Photography/
//When the page loads, Fade in all divs with class hidden.
//Then Remove hidden class.
$(document).ready(function () {
$('div.hidden').fadeIn(2000).removeClass('hidden');
});
//Fade In all h3 with class hidden.
//Remove hidden class.
$('h3.hidden').fadeIn(3000).removeClass('hidden');
//Image Filter
$(document).ready(function () {
$("#mainnav ul li a").click(function (){
var category = $(this).attr("class");
if (category == "all" ){
$("#imgcontainer img").addClass("hidden");
setTimeout(function (){
$("#imgcontainer img").removeClass("hidden");
});
}
else {
$("#imgcontainer img").addClass("hidden");
setTimeout(function(){
$("."+category).removeClass("hidden");
});
}
});
});
//When burger is clicked. Mobile nav and mainnav are toggled.
$(".burger").on("click", function (){
$("#mainnav").toggleClass("hidden");
$(".mobilenav").toggleClass("hidden");
});
body {
margin: 0;
padding: 0;
display: flex;
height: 100vh;
overflow: hidden;
font-family: 'Raleway', sans-serif;
}
ul {
list-style-type: none;
}
#container {
flex: 1 0 0;
overflow-y: auto;
}
/*------------------------Classes to be added to the current active link on a page----------*/
.active {
color: #766E6B;
}
/*--------------Side Navigation Styles--------------------*/
#sidenav {
background-color: black;
color: white;
width: 60px;
text-align: center;
border-right: 6px solid #766E6B;
overflow-y: auto;
}
#sidenav ul {
display: flex;
flex-direction: column;
padding: 0;
margin: 0;
justify-content: center;
}
#sidenav ul li:first-child {
margin-bottom: auto;
}
#sidenav ul li:last-child {
margin-top: auto;
}
#sidenav a {
padding: 20px;
display: block;
}
#sidenav a:visited {
color: white;
}
#sidenav a:hover {
color: black;
background-color: white;
}
/*-------------Header Styles-------------------------------*/
header {
padding: 40px 30px;
background-color: #F7F6F2;
}
header h1, h3 {
font-family: 'Tangerine', cursive;
}
header h1 {
font-size: 90px;
}
header h3 {
font-size: 40px;
}
header a {
text-decoration: none;
color: black;
padding: 0 0 12px 0;
line-height: 1.5em;
}
header a:hover {
transition: color 1s;
color: #766E6B;
}
.smalltxt {
font-size: 12px;
}
.topnavitems {
padding: 20px;
position: relative;
}
/*------------------Main Navigation-----------------------*/
#mainnav {
display: flex;
justify-content: center;
background-color: black;
}
#mainnav li {
margin-right: 5px;
}
#mainnav a {
color: white;
text-decoration: none;
letter-spacing: 1px;
padding: 10px;
}
#mainnav a:hover {
font-size: 20px;
color: grey;
}
/*--------------------ImgContainer Area------------------------*/
#imgcontainer {
display: flex;
flex-direction: row;
flex-wrap: wrap;
padding: 30px 20px;
justify-content: center;
background-color: #F7F6F2;
}
#imgcontainer img {
flex: 1;
width: 40vh;
padding: 10px;
}
#imgcontainer img:hover {
border: 2px solid black;
}
/*-----------------------Footer Styles---------------------*/
footer {
background-color: #F7F6F2;
padding: 10px 20px;
border-top: 1px solid grey;
}
footer ul {
justify-content: center;
}
footer li {
margin-right: 10px;
}
.developer {
text-decoration: none;
color: black;
}
/*-------------------Js Fade In class-------------------*/
.hidden {
display: none;
}
/*-------------------Flexbox-----------------------*/
.col {
flex: 1;
}
.row {
display: flex;
}
/*----------------------Mobile Navigation------------------------*/
.mobilenav {
background-color: black;
justify-content: center;
display: none;
}
.mobilenav ul {
display: flex;
flex-direction: row;
margin: 0;
}
.mobilenav li {
margin-right: 10px;
}
.mobilenav a {
color: white;
text-decoration: none;
display: block;
padding: 20px;
}
.mobilenav a:hover {
background-color: grey;
}
.burger {
background-color: black;
width: 100%;
text-align: right;
display: none;
cursor: pointer;
}
.burger i {
color: white;
padding: 5px;
margin-right: 10px;
}
/*-----------------Mobile Footer------------------------------*/
.mobilefooter {
display: none;
}
.mobilefooter a {
color: black;
}
/*-----------Media Queries----------------------------*/
@media screen and (max-width: 1024px){
#sidenav {
display: none;
}
nav.mobilenav.row {
display: none;
}
.mobilefooter {
display: flex;
}
}
@media screen and (max-width: 930px){
#imgcontainer img {
width: 80vh;
}
}
@media screen and (max-width: 740px){
header a {
display: none;
}
.mobilenav {
display: flex;
}
}
@media screen and (max-width: 600px){
header h1 {
font-size: 70px;
}
header h3 {
font-size: 40px;
}
#imgcontainer img {
width: 95%;
}
#mainnav {
display: block;
}
#mainnav ul {
flex-direction: column;
width: 100%;
margin: 0;
text-align: center;
padding: 0;
}
#mainnav li {
border-bottom: 1px solid grey;
padding: 10px;
}
.mobilenav {
display: block;
}
.mobilenav ul {
flex-direction: column;
width: 100%;
margin: 0;
text-align: center;
padding: 0;
}
.mobilenav li {
border-bottom: 1px solid grey;
}
.burger {
display: block;
}
}
<!DOCTYPE html>
<html>
<head>
<title>| J&D |</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--Custom CSS-->
<link rel="stylesheet" type="text/css" href="Styles/styles.css">
<!--Google Fonts-->
<link href="https://fonts.googleapis.com/css?family=Raleway:400i|Tangerine:700" rel="stylesheet">
</head>
<body>
<nav id="sidenav" class="row">
<ul class>
<li><a href="index.html"><i class="fa fa-home" aria-hidden="true"></i></a></li>
<li><a href="https://www.facebook.com" target="_blank"><i class="fa fa-facebook" aria-hidden="true"></i></a></li>
<li><a href="https://twitter.com/" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
<li><a href="https://www.pinterest.com/" target="_blank"><i class="fa fa-pinterest-p" aria-hidden="true"></i></a></li>
<li><a href="#"><i class="fa fa-envelope" aria-hidden="true"></i></a></li>
<li><a href="#"><i class="fa fa-arrow-up" aria-hidden="true"></i></a></li>
</ul>
</nav>
<div id="container">
<span class="burger"><a href="#" class="hamburger"><i class="fa fa-bars" aria-hidden="true"></i></a></span>
<nav class="mobilenav">
<ul>
<li><a href="index.html">Gallery</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Questions</a></li>
<li><a href="#">Rates</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<header>
<div class="row">
<div class="col">
<ul>
<li>
<div class="topnavitems hidden">
<a href="index.html">
<strong class="active">Gallery</strong>
<br>
<span class="smalltxt">Our Work</span>
</a>
</div>
</li>
<li>
<div class="topnavitems hidden">
<a href="#">
<strong>About</strong>
<br>
<span class="smalltxt">D&J Photography</span>
</a>
</div>
</li>
<li>
<div class="topnavitems hidden">
<a href="#">
<strong>Questions</strong>
<br>
<span class="smalltxt">Facts</span>
</a>
</div>
</li>
</ul>
</div>
<div class="col">
<h1>D&J Photography</h1>
<h3 class="hidden"><em>"Explore. Create. Inspire."</em></h3>
</div>
<div class="col">
<ul>
<li>
<div class="topnavitems hidden">
<a href="#">
<strong>Rates</strong>
<br>
<span class="smalltxt">Your Investment</span>
</a>
</div>
</li>
<li>
<div class="topnavitems hidden">
<a href="#">
<strong>Contact</strong>
<br>
<span class="smalltxt">Get In Touch</span>
</a>
</div>
</li>
</ul>
</div>
</div>
</header>
<span class="burger"><a href="#" class="hamburger"><i class="fa fa-bars" aria-hidden="true"></i></a></span>
<nav id="mainnav">
<ul class="row">
<li><a href="#" class="all">All</a></li>
<li><a href="#" class="food">Food</a></li>
<li><a href="#" class="people">People</a></li>
<li><a href="#" class="landmark">Landmarks</a></li>
<li><a href="#" class="nature">Nature</a></li>
<li><a href="#" class="sneakers">Sneakers</a></li>
</ul>
</nav>
<div id="imgcontainer">
<a href="Images/Food/FriedChicken.jpg"><img src="Images/Food/FriedChicken.jpg" class="food"></a>
<a href="Images/Nature/IcyMountains.jpg"><img src="Images/Nature/IcyMountains.jpg" class="nature"></a>
<a href="Images/Landmarks/EiffelTower.jpg"><img src="Images/Landmarks/EiffelTower.jpg" class="landmark"></a>
<a href="Images/People/Girl.jpg"><img src="Images/People/GuyInTrees.jpg" class="people"></a>
<a href="Images/Sneakers/GoldAndWhite.jpg"><img src="Images/Sneakers/GoldAndWhite.jpg" class="sneakers"></a>
<a href="Images/Food/ExoticDish.jpg"><img src="Images/Food/ExoticDish.jpg" class="food"></a>
<a href="Images/Nature/VastLandscape.jpg"><img src="Images/Nature/VastLandscape.jpg" class="nature"></a>
<a href="Images/Landmarks/LondonBridge.jpg"><img src="Images/Landmarks/LondonBridge.jpg" class="landmark"></a>
<a href="Images/People/GuyWithCap.jpg"><img src="Images/People/GuyWithCap.jpg" class="people"></a>
<a href="Images/Sneakers/Nike.jpg"><img src="Images/Sneakers/Nike.jpg" class="sneakers"></a>
<a href="Images/Food/ShrimpRice.jpg"><img src="Images/Food/ShrimpRice.jpg" class="food"></a>
<a href="Images/Nature/VeryGreenForest.jpg"><img src="Images/Nature/VeryGreenForest.jpg" class="nature"></a>
<a href="Images/Landmarks/RomanColosseum.jpg"><img src="Images/Landmarks/RomanColosseum.jpg" class="landmark"></a>
<a href="Images/People/OlderMan.jpg"><img src="Images/People/OlderMan.jpg" class="people"></a>
<a href="Images/Sneakers/Vans.jpg"><img src="Images/Sneakers/Vans.jpg" class="sneakers"></a>
<a href="Images/Sneakers/Yeezy.jpg"><img src="Images/Sneakers/Yeezy.jpg" class="sneakers"></a>
<a href="Images/Food/SteakTacos.jpg"><img src="Images/Food/SteakTacos.jpg" class="food"></a>
<a href="Images/Nature/MistyForest.jpg"><img src="Images/Nature/MistyForest.jpg" class="nature"></a>
<a href="Images/Landmarks/GermanyCastle.jpg"><img src="Images/Landmarks/GermanyCastle.jpg" class="landmark"></a>
<a href="Images/People/LittleGirl.jpg"><img src="Images/People/LittleGirl.jpg" class="people"></a>
</div>
<footer>
<ul class="row">
<li><p class="smalltxt">J&G Photography all rights reserved © 2017</p></li>
<li><p class="smalltxt">Designed and Developed by <strong><a href="http://jorgegoris.com/" class="developer">Jorge Goris</a></strong></p></li>
</ul>
<ul class="mobilefooter row">
<li><a href="https://www.facebook.com" target="_blank"><i class="fa fa-facebook" aria-hidden="true"></i></a></li>
<li><a href="https://twitter.com/" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
<li><a href="https://www.pinterest.com/" target="_blank"><i class="fa fa-pinterest-p" aria-hidden="true"></i></a></li>
<li><a href="#"><i class="fa fa-envelope" aria-hidden="true"></i></a></li>
</ul>
</footer>
</div>
<!--jQuery-->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<!--Font Awesome-->
<script src="https://use.fontawesome.com/d579f311e9.js"></script>
<!--Custom Js-->
<script src="js/custom.js"></script>
</body>
</html>
答案 0 :(得分:2)
隐藏的toggleClass无效,因为您在媒体查询中设置了CSS规则,如果max-size为600或740,则应用覆盖隐藏类属性的显示块或flex属性以使隐藏类工作那样做:
.hidden{
display: none !important;
}
我在这里添加了!important,因为你已经在#mainnav中设置了display属性,它比任何类(没有!important规则)具有更高的优先级,并覆盖了另一个类属性。