使用JQuery,我希望能够通过单击页面上的其他位置(任何不是汉堡)来折叠我的导航。我想我理解逻辑,但却无法使其发挥作用。
请参阅以下JFiddle。 https://jsfiddle.net/ywkxyjx3/33/
这是我到目前为止的逻辑:
$(".burger").click(function() {
$(".nav").toggleClass("show", 900); // toggles on .show css
$(".burger").toggleClass("change"); // toggles on .change css for hamburger animation
});
$('body').click(function(event){ //onBodyclick
if ($(!event.target).closest('.navCont').length) { //if click isnt
navCont or any child elements then..
return; //do nothing
} else {
// do something when not targeted
}
});
经过一些研究后,我发现了类似的问题,但我没有将它应用到我自己的自建导航中。
How to hide my collapse bootstrap 3 navbar with click on body when, collapse is visible?
How to close an open collapsed navbar when clicking outside of the navbar element in Bootstrap 3?
答案 0 :(得分:1)
查看以下代码。
$(".burger").click(function() {
$(".nav").toggleClass("show", 900);
$(".burger").toggleClass("change");
});
$('body').click(function(event){
if ($(!event.target).closest('.navCont').length) {
return; //do nothing
} else {
// do something when not targeted
}
});
$('html').click(function() {
$(".nav").removeClass("show", 900);
$(".burger").removeClass("change");
});
$('#navCont').click(function(event){
event.stopPropagation();
});

#navCont {
width: 47px;
height: auto;
float: right;
}
/*Dropdown Nav*/
.nav {
position: absolute;
display: block;
height: 0px;
float: right;
overflow: hidden;
top: 55px;
opacity: 99;
text-align: center;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.nav a {
color: black;
text-decoration: none;
}
.show {
height: auto;
}
.burger {
/* box around hamburger. Has onclick function*/
display: inline-block;
position: relative;
float: right;
padding: 5px;
cursor: pointer;
}
/*Individual lines of burger*/
.bar1,
.bar2,
.bar3 {
width: 36px;
height: 4px;
border-radius: 2px;
background-color: #95449a;
margin: 6px 0;
/*seperate the lines*/
transition: 1s;
/*time taken for any animation to take place */
}
/*in html classlist.toggle(change)*/
.change .bar1 {
-webkit-transform: translateY(10px) rotate(45deg);
transform: translateY(10px) rotate(45deg);
}
.change .bar2 {
opacity: 0;
-webkit-transform: translateX(10px);
transform: translateX(10px);
}
.change .bar3 {
-webkit-transform: translateY(-10px) rotate(-45deg);
transform: translateY(-10px) rotate(-45deg);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navCont">
<div class="burger">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<div class="nav">
<a href="#">Placeholder1</a>
<a href="#">Placeholder2</a>
<a href="#">Placeholder3</a>
</div>
</div>
&#13;
答案 1 :(得分:0)
试试这个:
order by DATE(SUBSTRING_INDEX(SUBSTRING_INDEX(field_name,'json_field_key1":"',-1),'"',1)) DESC
$(".burger").click(function(event) {
$(".nav").toggleClass("show", 900);
$(".burger").toggleClass("change");
event.stopPropagation();
});
$(document).click(function(event) {
if($('.change').length){
$(".nav").toggleClass("show", 900);
$(".burger").toggleClass("change");
}
});
#navCont {
width: 47px;
height: auto;
float: right;
}
/*Dropdown Nav*/
.nav {
position: absolute;
display: block;
height: 0px;
float: right;
overflow: hidden;
top: 55px;
opacity: 99;
text-align: center;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.nav a {
color: black;
text-decoration: none;
}
.show {
height: auto;
}
.burger {
/* box around hamburger. Has onclick function*/
display: inline-block;
position: relative;
float: right;
padding: 5px;
cursor: pointer;
}
/*Individual lines of burger*/
.bar1,
.bar2,
.bar3 {
width: 36px;
height: 4px;
border-radius: 2px;
background-color: #95449a;
margin: 6px 0;
/*seperate the lines*/
transition: 1s;
/*time taken for any animation to take place */
}
/*in html classlist.toggle(change)*/
.change .bar1 {
-webkit-transform: translateY(10px) rotate(45deg);
transform: translateY(10px) rotate(45deg);
}
.change .bar2 {
opacity: 0;
-webkit-transform: translateX(10px);
transform: translateX(10px);
}
.change .bar3 {
-webkit-transform: translateY(-10px) rotate(-45deg);
transform: translateY(-10px) rotate(-45deg);
}
答案 2 :(得分:0)
这是我的想法。
$(".burger").click(function(event) {
// Added this to make sure $('body').click doesn't override open nav function
event.stopPropagation()
$(".nav").toggleClass("show", 900);
$(".burger").toggleClass("change");
});
// then when you click the body, it checks to see whether or not it is already open, if it is, close it.
$('body').click(function(){
if ($(".nav").hasClass('show') && $(".burger").hasClass('change') ) {
$(".nav").toggleClass("show", 900);
$(".burger").toggleClass("change");
}
})