2
This is my first post and I have a question. I am working on a dropdown menu that acts based on user click. This way, when clicking on one of the menu items, it descends presenting more information, and when you click again it is closed (the codes are at the end of the post).
1: The first problem is that I want the menu to be closed by clicking anywhere on the page, and not just by clicking again on the menu button. So he wanted some function that captured the mouse click on the page. I don’t know if that’s the best solution, if not, let me know how I can do it.
2: The other problem would be the fact that the dropdown menu that is inserted in the first menu item and only works after the first click, that is, only subsequent clicks cause it to close and open the menu correctly. What I thought was that Javascript with the variable that captures the information is not updating until the moment of the first click, so it only works from the second. I don’t know if I’m right, I looked for a way to execute the code only when everything was loaded, I got to the function document.onload()
, but it didn’t work for me. I don’t know if I made a mistake or if that wasn’t the solution. Anyway, these are my two doubts. I will thank you very much if someone can help, it will greatly enhance my learning.
var elem = document.getElementsByClassName('sub-menu');
function fclick(){
if(elem[0].style.display == "none"){
elem[0].style.display = "block";
}else{
elem[0].style.display = "none";
}
}
*{color: #fff; margin: 0; padding: 0;}
body{background-color: #f5f5f5;}
a{text-decoration: none;}
li{list-style: none;}
#navbar{
background-color: #2C353B;
height: 3em;
}
.menu{
display: block;
}
.menu li{
float: left;
padding: 13px 10px;
}
.sub-menu{
position: absolute;
display: none;
top: 47px;
left: 0;
}
.sub-menu li{
float: none;
padding: 13px 10px;
background-color: #2C353B;
}
<!DOCTYPE html>
<html lang="pt-BR" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<title>clickable-menu</title>
</head>
<body>
<nav id="navbar">
<ul class="menu">
<li>
<a href="#" onclick="fclick()">Some-item</a>
<ul class="sub-menu">
<li><a href="#">Some-item</a></li>
<li><a href="#">Some-item</a></li>
<li><a href="#">Some-item</a></li>
</ul>
</li>
<li><a href="#">Some-item</a></li>
<li><a href="#">Some-item</a></li>
</ul>
</nav>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
Very good, I could understand, so even though I can manipulate the css by javascript, it doesn’t mean that it already has the feature that I’m changing by default changed, I need to flag this in the code, very cool, I didn’t know these details, your reply was very conclusive, I will take advantage and study on these details you mentioned mainly on the function stopPropagation() that I did not know, very grateful now I can re-edit and improve my code.
– Bruno Pinheiro
Cool! But it’s the other way around: CSS cannot change element properties, Javascript can.
– Sam