我正在使用侧边栏菜单,但是当点击汉堡菜单时它没有打开=。显示此错误: 无法读取null(...)
的属性'className'
document.addEventListener("DOMContentLoaded", function(event) {
$('#nav-icon').click(function() {
$(this).toggleClass('open');
if (this.className) {
//menu is open right now
var getClass = document.querySelector(".desktop-nav").className;
var updateClass = ".desktop-nav" + " desktop-nav-open";
document.querySelector(".desktop-nav").className = updateClass;
} else {
//menu is closed right now
var getClass = document.querySelector(".desktop-nav").className;
var updateClass = ".desktop-nav";
document.querySelector(".desktop-nav").className = updateClass;
}
});
});
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
div.full-width-div {
width: 100%;
margin: 0;
padding: 0;
}
.container {
width: 100%;
margin: 0;
padding: 0;
}
div#mainrow {
width: 100%;
margin: 0;
padding: 0;
display: flex;
}
div#right-content {
margin: 0;
padding: 0;
width: 100%;
}
div.maincol {
margin: 0;
padding: 0;
}
div#desktop-nav-wrap {
height: 100%;
position: relative;
background-color: #212331;
}
div.desktop-nav {
height: 100vh;
position: fixed;
width: 200px;
top: 0;
left: 0;
background-color: #212331;
z-index: 10;
-webkit-transition: left 0.8s ease-out, padding 0s ease-in-out;
-moz-transition: left 0.8s ease-out, padding 0s ease-in-out;
transition: left 0.8s ease-out, padding 0s ease-in-out;
}
div.mobile-nav {
height: 100vh;
position: fixed;
width: 65px;
top: 0;
left: 0;
background-color: #212331;
z-index: 10;
visibility: hidden;
-webkit-transition: left 0.8s ease-out, padding 0s ease-in-out;
-moz-transition: left 0.8s ease-out, padding 0s ease-in-out;
transition: left 0.8s ease-out, padding 0s ease-in-out;
}
@media (max-width: 991px) {
div.desktop-nav {
left: -260px;
}
div.desktop-nav-open {
left: 65px;
}
div.mobile-nav {
visibility: visible;
}
div.mobile-nav-open {
left: 200px;
}
}
@media (min-width: 768px) {
div.mobile-nav {
width: 70px;
}
}
@media (min-width: 1150px) {
div.desktop-nav {
width: 250px;
}
}
div#logo-div {
width: 100%;
}
div#logo-div img {
display: block;
width: 100%;
}
div#desktop-navigation {
width: 100%;
margin: 0;
padding: 0;
}
div#desktop-navigation>ul {
width: 100%;
margin: 0;
padding: 0;
margin-top: 30px;
}
div#desktop-navigation>ul>li {
width: 100%;
margin: 0;
padding: 0;
list-style-type: none;
}
div#desktop-navigation>ul>li>a {
width: 100%;
margin: 0;
padding: 0;
color: white;
font-family: 'Dosis';
text-transform: uppercase;
text-align: center;
letter-spacing: 3px;
line-height: 45px;
display: block;
font-size: 1em;
}
div#desktop-navigation>ul>li>a:hover,
div#desktop-navigation>ul>li>a:visited {
text-decoration: none;
background-color: #323445;
}
#nav-icon {
width: 35px;
height: 30px;
position: relative;
margin: 20px auto;
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transition: .5s ease-in-out;
-moz-transition: .5s ease-in-out;
-o-transition: .5s ease-in-out;
transition: .5s ease-in-out;
cursor: pointer;
}
@media (min-width: 768px) {
#nav-icon {
width: 40px;
}
}
#nav-icon span {
display: block;
position: absolute;
height: 2px;
width: 100%;
background: #d3531a;
border-radius: 9px;
opacity: 1;
left: 0;
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transition: .25s ease-in-out;
-moz-transition: .25s ease-in-out;
-o-transition: .25s ease-in-out;
transition: .25s ease-in-out;
}
#nav-icon span:nth-child(1) {
top: 0px;
}
#nav-icon span:nth-child(2) {
top: 12px;
}
#nav-icon span:nth-child(3) {
top: 24px;
}
#nav-icon.open span:nth-child(1) {
top: 12px;
-webkit-transform: rotate(135deg);
-moz-transform: rotate(135deg);
-o-transform: rotate(135deg);
transform: rotate(135deg);
}
#nav-icon.open span:nth-child(2) {
opacity: 0;
left: -60px;
}
#nav-icon.open span:nth-child(3) {
top: 12px;
-webkit-transform: rotate(-135deg);
-moz-transform: rotate(-135deg);
-o-transform: rotate(-135deg);
transform: rotate(-135deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="row" id="mainrow">
<div class="col-md-2 col-sm-1 col-xs-1 maincol">
<div id="desktop-nav-wrap">
<div class="desktop-nav">
<div id="logo-div">
<a href="index.html">
<img src="img/logo.png" />
</a>
</div>
<div id="desktop-navigation">
<ul>
<li><a href="about.html">About</a>
</li>
<li><a href="events.html">Events</a>
</li>
<li><a href="calendar.html">Calendar</a>
</li>
<li><a href="insights.html">Insights</a>
</li>
<li><a href="resources.html">Resources</a>
</li>
<li><a href="news.html">News</a>
</li>
<li><a href="team.html">Out Team</a>
</li>
<li><a href="contact.html">Contact</a>
</li>
</ul>
</div>
</div>
<div class="mobile-nav">
<div id="nav-icon">
<span></span>
<span></span>
<span></span>
</div>
</div>
</div>
</div>
<div class="col-md-10 col-sm-11 col-xs-11 maincol">
</div>
</div>
<!--end of row-->
</div>
<!--end of container-->
答案 0 :(得分:0)
您需要做的是在&#39; eventListener&#39;之外声明一个函数。所以它不会显示你得到的错误:
像这样更新你的javaScript:
document.addEventListener("DOMContentLoaded", function(event) {
//function that updates classes on passing a class
var classUpdate = function (queryClass,newClasses){
var qClass = "." + queryClass;
var gClass = document.querySelector(qClass).className;
var uClass = queryClass + " " + newClasses;
document.querySelector(qClass).className = uClass;
}
$('#nav-icon').click(function(){
$(this).toggleClass('open');
if(this.className){
var queryClass = "desktop-nav";
var newClasses = "desktop-nav-open";
classUpdate(queryClass,newClasses);
}
else{
var queryClass = "desktop-nav";
var newClasses = "";
classUpdate(queryClass,newClasses);
}
});
});
答案 1 :(得分:0)
由于您已经在使用jQuery
,因此也可以通过更简单的方式完成此操作。
$(function() {
$('#nav-icon').click(function() {
$(this).toggleClass('open');
$(".desktop-nav").toggleClass("desktop-nav-open")
});
});
&#13;
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
div.full-width-div {
width: 100%;
margin: 0;
padding: 0;
}
.container {
width: 100%;
margin: 0;
padding: 0;
}
div#mainrow {
width: 100%;
margin: 0;
padding: 0;
display: flex;
}
div#right-content {
margin: 0;
padding: 0;
width: 100%;
}
div.maincol {
margin: 0;
padding: 0;
}
div#desktop-nav-wrap {
height: 100%;
position: relative;
background-color: #212331;
}
div.desktop-nav {
height: 100vh;
position: fixed;
width: 200px;
top: 0;
left: 0;
background-color: #212331;
z-index: 10;
-webkit-transition: left 0.8s ease-out, padding 0s ease-in-out;
-moz-transition: left 0.8s ease-out, padding 0s ease-in-out;
transition: left 0.8s ease-out, padding 0s ease-in-out;
}
div.mobile-nav {
height: 100vh;
position: fixed;
width: 65px;
top: 0;
left: 0;
background-color: #212331;
z-index: 10;
visibility: hidden;
-webkit-transition: left 0.8s ease-out, padding 0s ease-in-out;
-moz-transition: left 0.8s ease-out, padding 0s ease-in-out;
transition: left 0.8s ease-out, padding 0s ease-in-out;
}
@media (max-width: 991px) {
div.desktop-nav {
left: -260px;
}
div.desktop-nav-open {
left: 65px;
}
div.mobile-nav {
visibility: visible;
}
div.mobile-nav-open {
left: 200px;
}
}
@media (min-width: 768px) {
div.mobile-nav {
width: 70px;
}
}
@media (min-width: 1150px) {
div.desktop-nav {
width: 250px;
}
}
div#logo-div {
width: 100%;
}
div#logo-div img {
display: block;
width: 100%;
}
div#desktop-navigation {
width: 100%;
margin: 0;
padding: 0;
}
div#desktop-navigation>ul {
width: 100%;
margin: 0;
padding: 0;
margin-top: 30px;
}
div#desktop-navigation>ul>li {
width: 100%;
margin: 0;
padding: 0;
list-style-type: none;
}
div#desktop-navigation>ul>li>a {
width: 100%;
margin: 0;
padding: 0;
color: white;
font-family: 'Dosis';
text-transform: uppercase;
text-align: center;
letter-spacing: 3px;
line-height: 45px;
display: block;
font-size: 1em;
}
div#desktop-navigation>ul>li>a:hover,
div#desktop-navigation>ul>li>a:visited {
text-decoration: none;
background-color: #323445;
}
#nav-icon {
width: 35px;
height: 30px;
position: relative;
margin: 20px auto;
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transition: .5s ease-in-out;
-moz-transition: .5s ease-in-out;
-o-transition: .5s ease-in-out;
transition: .5s ease-in-out;
cursor: pointer;
}
@media (min-width: 768px) {
#nav-icon {
width: 40px;
}
}
#nav-icon span {
display: block;
position: absolute;
height: 2px;
width: 100%;
background: #d3531a;
border-radius: 9px;
opacity: 1;
left: 0;
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transition: .25s ease-in-out;
-moz-transition: .25s ease-in-out;
-o-transition: .25s ease-in-out;
transition: .25s ease-in-out;
}
#nav-icon span:nth-child(1) {
top: 0px;
}
#nav-icon span:nth-child(2) {
top: 12px;
}
#nav-icon span:nth-child(3) {
top: 24px;
}
#nav-icon.open span:nth-child(1) {
top: 12px;
-webkit-transform: rotate(135deg);
-moz-transform: rotate(135deg);
-o-transform: rotate(135deg);
transform: rotate(135deg);
}
#nav-icon.open span:nth-child(2) {
opacity: 0;
left: -60px;
}
#nav-icon.open span:nth-child(3) {
top: 12px;
-webkit-transform: rotate(-135deg);
-moz-transform: rotate(-135deg);
-o-transform: rotate(-135deg);
transform: rotate(-135deg);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="row" id="mainrow">
<div class="col-md-2 col-sm-1 col-xs-1 maincol">
<div id="desktop-nav-wrap">
<div class="desktop-nav">
<div id="logo-div">
<a href="index.html">
<img src="img/logo.png" />
</a>
</div>
<div id="desktop-navigation">
<ul>
<li><a href="about.html">About</a>
</li>
<li><a href="events.html">Events</a>
</li>
<li><a href="calendar.html">Calendar</a>
</li>
<li><a href="insights.html">Insights</a>
</li>
<li><a href="resources.html">Resources</a>
</li>
<li><a href="news.html">News</a>
</li>
<li><a href="team.html">Out Team</a>
</li>
<li><a href="contact.html">Contact</a>
</li>
</ul>
</div>
</div>
<div class="mobile-nav">
<div id="nav-icon">
<span></span>
<span></span>
<span></span>
</div>
</div>
</div>
</div>
<div class="col-md-10 col-sm-11 col-xs-11 maincol">
</div>
</div>
<!--end of row-->
</div>
<!--end of container-->
&#13;