Reputation: 77
I'm using a piece of JS for an awesome little button I found online. The button works beautifully, but it doesn't hide the menu when I click on the page. I have to click the menu button to hide the menu again.
I've looked around a bit and see other threads like this, but my limited understanding of JS has me limited as to what I can do on my own.
$(function() {
var menuVisible = false;
$('.menuBtn').click(function() {
if (menuVisible) {
$('.myMenu').css({'display':'none'});
menuVisible = false;
return;
}
$('.myMenu').css({'display':'block'});
menuVisible = true;
});
$('.myMenu').click(function() {
$(this).css({'display':'none'});
menuVisible = false;
});
});
What do I add to this code, and how do I phrase it, to make the menu disappear when I click anywhere but the menu/button?
I only come here as a last resort. Everything I've tried breaks functionality. Thanks for your help! :)
Upvotes: 0
Views: 220
Reputation: 24001
simply you can use this function
// when click on window
function actionwindowclick(elem , action){
$(window).on('click',function(e){
if (!$(elem).is(e.target) // if the target of the click isn't the container...
&& $(elem).has(e.target).length === 0) // ... nor a descendant of the container
{
action();
}
});
}
and use it inside $(function(){});
like
actionwindowclick('.myMenu , .menuBtn' , function(){
$('.myMenu').hide();
});
your full code should be something like this
$(function() {
$('.menuBtn').click(function() {
$('.myMenu').slideToggle(0);
});
$('.myMenu').click(function() {
//$(this).hide();
});
actionwindowclick('.myMenu , .menuBtn' , function(){
$('.myMenu').hide();
});
});
// when click on window
function actionwindowclick(elem , action){
$(window).on('click',function(e){
if (!$(elem).is(e.target) // if the target of the click isn't the container...
&& $(elem).has(e.target).length === 0) // ... nor a descendant of the container
{
action();
}
});
}
Upvotes: 0
Reputation: 1743
$(function() {
var menuVisible = false;
$('.menuBtn').click(function() {
if (menuVisible) {
$('.myMenu').css({'display':'none'});
menuVisible = false;
return;
}
$('.myMenu').css({'display':'block'});
menuVisible = true;
});
$('.myMenu').click(function() {
$(this).css({'display':'none'});
menuVisible = false;
});
$(document).on('click',function(e){
// if the click is made any where on body except .menuBtn element
// then hide the menubar
if($(e.target).closest('.menuBtn').length === 0){
$('.myMenu').css({'display':'none'});
menuVisible = false;
}
});
});
.menuBtn {
background-color: cornflowerblue;
font-size:1.1em;
padding:7px 15px;
display:inline-block;
margin: 7px;
border-radius:6px;
border:solid rgba(0,0,0,0.2);
border-width:1px 1px 5px;
box-shadow:0 5px 0 rgba(0,0,0,0.1),
inset 0 0 3px rgba(255,255,255,0.3);
cursor:pointer;
transition:0.4s ease;
}
.menuBtn:hover {
transform:translateY(-3px);
box-shadow:0 6px 0 rgba(0,0,0,0.1), inset 0 0 1px rgba(255,255,255,0.4);
border-bottom-width:8px; }
.menuBtn:active { transform:translateY(4px); box-shadow:0 2px 0 rgba(0,0,0,0.1), inset 0 0 5px rgba(255,255,255,0.4); border-bottom-width:2px; transition:0.1s ease; }
.wrapper {
position: relative;
display: inline-block;
}
.myMenu {
display: none;
position: absolute;
background-color: white;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.6);
}
.myMenu a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.myMenu a:hover {background-color: cornflowerblue; color: yellow;}
.wrapper:hover {
display: block;
}
.myMenu {
display: none;
}
.menuBtn:hover .menuBtn {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<button class="menuBtn"></button>
<div class="myMenu">
<a href="#">Login</a>
<a href="#">Register</a>
<a href="#">About Us</a>
<a href="#">About the Crate</a>
<a href="#">About Us</a>
</div>
</div>
Upvotes: 1
Reputation: 1
I do not know if I understand well , more would be what you need:
http://www.w3schools.com/howto/howto_js_dropdown.asp
Upvotes: 0