I'm building a responsive website with a top navigation bar. When the window is smaller than 919px, the bar disappears and the MENU button appears (which is normally hidden with {display: none;}). On click, it opens a drop-down menu with a cross that closes the drop-down and brings back the MENU button:
<script>
function myFunction() {
$(document).ready(function(){
$(".icon").click(function(){
$(".icon").hide();
$("#dropdown").show();
});
});
}
</script>
<script>
function myFunction2() {
$(document).ready(function(){
$("#cross").click(function(){
$("#dropdown").hide();
$(".icon").show();
});
});
}
</script>
This all works fine in the small version of the website. However, as soon as I've clicked the MENU button at least once and resized the website back to larger than 919px, the button or the dropdown menu (depending on whichever one is open) doesn't disappear any more, unless I refresh the page.
I understand that jQuery's .show()somehow overrides the {display: none;} from my CSS, but I can't figure out how to prevent it. I'll be very thankful for any ideas on how to fix this! I'm new to this and will be really happy to learn a way around this.
EDIT
$(document).ready(function(){
$("#iconmenulink").click(function(){
$("#iconmenu").addClass("dropdownnav_hidden");
$("#dropdown").addClass("dropdownnav_shown");
});
});
$(document).ready(function(){
$("#cross").click(function(){
$("#dropdown").addClass("dropdownnav_hidden");
$("#iconmenu").addClass("dropdownnav_shown");
});
});
.dropdownnav_shown {
display:block;
position: absolute;
right: 28px;
top:29px;
float:right;
background-color:white;
border: 2px solid;
border-color:#8A2BE2;
text-decoration: none;
padding:0;
}
.dropdownnav_hidden {
display:none;
}
.dropdownnav_shown ul {
list-style-type: none;
transition: 0.3s;
font-family: 'Karla', sans-serif;
font-weight: bold;
font-size: 16px;
color: black;
padding-left:20px;
padding-right:10px;
line-height:150%;
margin-top:5px;
margin-bottom:10px;
}
.dropdownnav_shown ul li a {
text-decoration: none;
color:black;
margin-right:10px;
}
.dropdownnav_shown ul li a:visited {color: black;}
.dropdownnav_shown ul li a:hover {color:#8A2BE2;}
#iconmenu a {
line-height:100%;
padding:0;
vertical-align:bottom;
}
#cross {font-size: 22px;
text-align:right;
margin-bottom:5px;
}
#cross a {color: #8A2BE2;
}
<div class="dropdownnav_shown" id="iconmenu">
<ul>
<li id="iconmenulink"> <a> MENU </a>
<li>
</ul>
</div>
<div class ="dropdownnav_hidden" id="dropdown">
<ul>
<li id="cross"> <a href="#cross" onclick="myFunction2()"> × </a></li>
<li><a href="#recordings">RECORDINGS</a></li>
<li><a href="#news">NEWS</a></li>
<li><a href="#cv">CV</a></li>
<li><a href="mailto:[email protected]">CONTACT</a></li>
<li><a href="https://facebook.com/123" target=_blank>FB</a></li>
</ul>
</div>
display:noneinline to the element. If your CSS would be stronger than that (say you add!importantto the rule in your CSS) it would be stronger but than$.hide()method wouldn't work as only the strongest CSS rule applies. It would technically work (the element would get an inline property ofdisplay:none) but it wouldn't apply. What you probably need to do is make a function that could decide on whether the element should or should not be displayed and run that function on pageresizeevent.