Jquery dropdown menu click always open - javascript

I created a dropdown menu click, but it have a little weirdness. When I click the button dropdown, the dropdown menu has appear. But when I move my cursor to another (without click the button dropdown again), the dropdown menu dissapear and it has become hoverable dropdown menu not dropdown menu click (Sorry for my bad English)
How can I make the dropdown menu click always appear when I click the button dropdown and move the cursor?
(Here is my code)
HTML
<aside class="sidebar">
<ul>
<li><i class="material-icons">home</i>Homepage</li>
<li class="button_dropdown"><i class="material-icons">widgets</i>Events Organizer <i class="material-icons multi_menu">keyboard_arrow_right</i>
<ul class="dropdown_menu">
<li>Create Events</li>
<li>List Events</li>
</ul>
</li>
<li><i class="material-icons">people</i>Peserta Events</li>
</ul>
</aside>
CSS
aside.sidebar ul li ul.dropdown_menu {
opacity: 0;
visibility: hidden;
height: auto;
width: 100%;
margin-top: -1px;
position: absolute;
transition: all 0.5s;
border-left: 1px solid #2c3e50;
background-color: #34495e;
}
aside.sidebar ul li ul.dropdown_menu.active {
opacity: 1;
visibility: visible;
height: auto;
width: 100%;
background-color: #34495e;
left: 100%;
top: 0;
transition: all 0.5s;
}
Jquery
$(document).ready(function () {
$(".button_dropdown").click(function () {
$(".dropdown_menu").toggleClass("active");
});
});

I personally would use hover rather than click for a child menu. Let me know how you go with this. Stays active until clicked out.
aside.sidebar ul li ul.dropdown_menu {
display: none;
height: auto;
width: 100%;
margin-top: -1px;
position: absolute;
transition: all 0.5s;
border-left: 1px solid #2c3e50;
background-color: #34495e;
left:200px;
top:0;
}
aside.sidebar ul li ul.dropdown_menu.active {
display: block !important;
}
Working in this snippet.
$(document).ready(function() {
$('.button_dropdown').click(function() {
$('.dropdown_menu').toggleClass('active');
});
});
aside.sidebar ul li ul.dropdown_menu {
display: none;
height: auto;
width: 100%;
margin-top: -1px;
position: absolute;
transition: all 0.5s;
border-left: 1px solid #2c3e50;
background-color: #34495e;
left:200px;
top:0;
}
aside.sidebar ul li ul.dropdown_menu.active {
display: block !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<aside class="sidebar">
<ul>
<li>Homepage</li>
<li class="button_dropdown">Events Organizer
<ul class="dropdown_menu">
<li>Create Events</li>
<li>List Events</li>
</ul>
</li>
<li>Peserta Events</li>
</ul>
</aside>

Without changing your code too much, you can just remove the pointer-events (clicks, etc.) by adding:
pointer-events:none; to aside.sidebar ul li ul.dropdown_menu
and
pointer-events:auto; to aside.sidebar ul li ul.dropdown_menu.active

The hoverable dropdown menu is because you have set the opacity property to 0 in your css aside (dropdown_menu). You must change opacity:0 to opacity:1. Here is your code with error:
aside.sidebar ul li ul.dropdown_menu {
opacity: 0;
visibility: hidden;
height: auto;
width: 100%;
margin-top: -1px;
position: absolute;
transition: all 0.5s;
border-left: 1px solid #2c3e50;
background-color: #34495e;
}
Replace by (fixed opacity):
aside.sidebar ul li ul.dropdown_menu {
opacity: 1;
visibility: hidden;
height: auto;
width: 100%;
margin-top: -1px;
position: absolute;
transition: all 0.5s;
border-left: 1px solid #2c3e50;
background-color: #34495e;
}

Related

How to achieve a css pseudo-element based and transition based tab-navigation behavior and styling?

How can I implement a functioning tab-navigation behavior?
What I want to achieve is a underlying animated bar for each navigation-item that has been clicked.
The transition-related styling is supposed to be based on css pseudo-elements.
Below is the markup and the css-rules I came up with so far.
What am I missing in my code? How could the approach be fixed?
li::after {
border-radius: 2px;
border-bottom: red solid 3px;
transition: all .3s ease-in-out;
}
li::before {
content: "";
}
<nav>
<ul class="nav_link">
<strong>
<li onclick="tabs('avisos')">Avisos</li>
<li onclick="tabs('atividades')">Atividades</li>
<li onclick="tabs('trabalhos')">Trabalhos</li>
<li onclick="tabs('provas')">Provas</li>
<li onclick="tabs('aulas')">Aulas</li>
</strong>
</ul>
</nav>
Regarding the provided ::after rule there is no definition of how the pseudo-element should be display-ed in addition to the also missing content attribute.
And for the entire example as is, there is no need for a JavaScript based solution.
A css-only variant of the OP's code could use e.g.
a :hover based approach.
And regarding the ::before/::after pseudo-element based transition-effect
height, in my opinion, is a more intuitive attribute to go for.
.nav_link {
width: 20%;
list-style-type: none;
font-weight: bolder;
}
.nav_link li {
position: relative;
margin: 5px 0 7px 0;
}
.nav_link li::after {
content: "";
position: absolute;
left: -5px;
top: 100%;
width: 100%;
height: 0;
background-color: red;
transition: height .3s ease-in-out;
}
.nav_link li:hover {
cursor: pointer;
}
.nav_link li:hover::after {
height: 2px;
}
<nav>
<ul class="nav_link">
<li>Avisos</li>
<li>Atividades</li>
<li>Trabalhos</li>
<li>Provas</li>
<li>Aulas</li>
</ul>
</nav>
Due to the markup, provided by the OP, the above example does not support a behavior similar to the OP's intended script-based click handling.
A small markup change could solve this though and support tab navigation too.
.nav_link {
width: 20%;
list-style-type: none;
font-weight: bolder;
}
.nav_link a {
display: block;
width: 100%;
position: relative;
margin: 5px 0 7px 0;
padding-left: 5px;
}
.nav_link a::after {
content: "";
position: absolute;
left: 0;
top: 100%;
width: 100%;
height: 0;
background-color: red;
transition: height .3s ease-in-out;
}
.nav_link a:hover {
cursor: pointer;
}
.nav_link a:hover,
.nav_link a:focus {
outline: 1px dashed red;
}
.nav_link a,
.nav_link a:hover,
.nav_link a:focus,
.nav_link a:active,
.nav_link a:visited {
color: black;
text-decoration: none;
}
.nav_link a:target::after,
.nav_link a:focus::after,
.nav_link a:active::after {
height: 2px;
}
<nav>
<ul class="nav_link">
<li>
<a name="avisos" href="#avisos">Avisos</a></li>
<li>
<a name="atividades" href="#atividades">Atividades</a>
</li>
<li>
<a name="trabalhos" href="#trabalhos">Trabalhos</a>
</li>
<li>
<a name="provas" href="#provas">Provas</a>
</li>
<li>
<a name="aulas" href="#aulas">Aulas</a>
</li>
</ul>
</nav>
You're probably looking for li:active or li:visited
Note that when using li:active, the item will only be underlined while the mouse button is down.
More here

Javascript help: "slide-revealing" menu

Current setup (plain HTML/CSS):
I've currently got this plain HTML/CSS setup, which is basically using a checkbox with no opacity, with labels acting as buttons (which they in fact are not).
Codepen: https://codepen.io/MikaTheDesigner/pen/MWVYGoz
Video of my current HTML/CSS-demo (and the result goal): https://i.imgur.com/ha3NL0V.mp4
<div class="nav">
<input class="menuBtn" type="checkbox">
<label class="menuLabel open">Menu</label>
<label class="menuLabel close">Close</label>
<div class="nav menuBox transitionBox menuTransition"></div>
<div class="nav menuBox BG">
<nav>
<ul>
<li><a>Option 1</a></li>
<li><a>Option 2</a></li>
<li><a>Option 3</a></li>
</ul>
</nav>
</div>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.nav {
pointer-events: none;
position: fixed;
z-index: 100;
width: 100vw;
height: 100vh;
}
.nav > .menuBtn {
cursor: pointer;
width: 122.43px;
height: 122.43px;
margin: 0 0 0 3rem;
pointer-events: initial;
position: absolute;
z-index: 99;
opacity: 0;
}
.nav > .menuBtn:checked ~ .menuLabel.open {opacity: 0;}
.nav > .menuBtn:checked ~ .menuLabel.close {opacity: 100%;}
.nav > .menuBtn:checked ~ .menuBox.transitionBox {left: 100%;}
.nav > .menuBtn:checked ~ .menuBox.BG {left: 0;}
.nav > .menuLabel {
color: black;
font-size: 1.5rem;
position: absolute;
z-index: 98;
margin: 3rem 0 0 3rem;
text-align: center;
transition: all 0.2s ease-in-out;
}
.nav > .menuLabel.open {
text-shadow: 0 0 2rem rgba(0,0,0,.5);
width: 122.43px;
}
.nav > .menuLabel.close {
opacity: 0;
}
.nav > .menuBox.transitionBox {
background-color: black;
width: 200%;
height: 100%;
position: absolute;
z-index: 100;
left: -200%;
transition: all 2000ms;
}
.nav > .menuBox.BG {
background: linear-gradient(to bottom, white, black);
background-size: cover;
width: 100%;
height: 100%;
pointer-events: auto;
position: absolute;
z-index: 96;
left: -100%;
transition-delay: 500ms !important;
transition: all 200ms;
}
.nav > .menuBox.BG > nav {
position: absolute;
z-index: 97;
width: 100%;
height: 100%;
}
.nav > .menuBox.BG > nav > ul {
list-style: none;
padding: 122.43px 3rem 3rem calc(6rem + 122.43px);
}
.nav > .menuBox.BG > nav > ul li {
color: white;
font-size: 2rem;
line-height: 2rem;
margin-bottom: 1.5rem;
}
.nav > .menuBox.BG > nav > ul li > a {
color: inherit;
display: block;
text-decoration: none;
transition: all 0.2s ease-in-out;
width: max-content;
}
.nav > .menuBox.BG > nav > ul li > a:hover {cursor: pointer;}
Goal:
My goal is for the menu to act in the exact same way, when clicking the labels .menulabel.open and .menuLabel.close, but using javascript instead of plain HTML/CSS.
I would change these current labels to a-tags or p-tags and using onClick-functions, when I get the javascript working.
Like linked at the top of the thread, this is my goal, but using javascript to make it react, and not using a plain checkbox:
https://i.imgur.com/ha3NL0V.mp4
What have I tried so far?
Besides the plain HTML/CSS-solution I have tried setting up, which I wouldn't argue is the right way to make the menu, I have also tried setting this script up in my HTML-document, inwhich does not seem to work as I want it to:
function openNav() {
document.getElementsByClassName("menuTransition").style.left = "100%";
document.getElementsByClassName("menuBox").style.left = "0";
}
function closeNav() {
document.getElementsByClassName("menuTransition").style.left = "-200%";
document.getElementsByClassName("menuBox").style.left = "-100%";
}
(the javascript was supposed to just style the two elements when clicking on one of the a-tags the exact same way the CSS reacts, when checking the checkbox and "activating" the menu)
<div class="nav">
<a class="menuLabel open" onClick="openNav()">Menu</a>
<div class="nav menuBox transitionBox menuTransition"></div>
<div class="nav menuBox BG">
<a class="menuLabel close" onClick="closeNav()">Close</a>
<nav>
<ul>
<li><a>Option 1</a></li>
<li><a>Option 2</a></li>
<li><a>Option 3</a></li>
</ul>
</nav>
</div>
</div>
(basically the same HTML as above, just removing the labels and replacing them with a-tags)
You can use a single class and toggle that class on the click of a button, something like this:
function myFunction() {
var element = document.getElementById("myDIV");
element.classList.toggle("mystyle");
}
.mystyle {
width: 100%;
padding: 25px;
background-color: coral;
color: white;
font-size: 25px;
box-sizing: border-box;
}
<button onclick="myFunction()">Try it</button>
<div id="myDIV">
This is a DIV element.
</div>
document.getElementsByClassName("menuBox") return an array object .
you need to add the index , such as document.getElementsByClassName("menuBox")[0]

CSS horizontal submenu

I'm working on the navigation bar for a website and currently the main menu is complete. However, the "Services" and "Products" buttons need to each have their own sub-menu. The sub-menu should normally be hidden from view and appears when the user mouse-overs on the respective button.
Here is a fiddle with the desired result. Obviously, I'd rather not use any javascript if possible.
The idea I had initially was to have sub-menu have position: absolute with a z-index value lower than that of the main-menu, so that it can slide underneath the main-menu. However, doing so messes up with the width if I give it width: 100% and since my site is responsive, I avoid static widths.
I also tried doing with relative positioning, but that doesn't work either.
Another thing I don't like with that approach is that the markup for the main menu and sub-menu get split. Is it possible to get the above result, but with this markup?
<nav>
<ul class="nav">
<li role="presentation" class="active">Home</li>
<li role="presentation">Services
<ul>
<li role="presentation">Link 1
<li role="presentation">Link 2
</ul>
</li>
<li role="presentation">Products
<ul>
<li role="presentation">Link 3
<li role="presentation">Link 4
</ul>
</li>
<li role="presentation">About</li>
<li role="presentation">Contact</li>
</ul>
</nav>
Here is my code:
CSS
body {
font-size: 0;
}
.bodyframe {
display: inline-block;
box-shadow: 0 6px 15px rgba(0, 0, 0, 0.6);
}
.div_container {
max-width: 1460px;
width: 100%;
display: inline-block;
margin: 0 auto;
}
header {
width: 100%;
height: 49px;
}
.nav {
display: block;
position: relative;
list-style: none;
background: #304770;
z-index: 10;
}
.nav li {
display: inline-block;
background-color: #304770;
margin: 0 5px;
}
.nav li a {
padding: 12px 15px;
font-size: 18px;
color: #EFEFEF;
display: block;
}
.nav li.active a {
color: orange;
}
.nav li.active a:before {
width: 100%;
}
.nav li a:hover {
background-color: #304770;
color: orange;
transition: color 0.25s;
}
.nav li a:before {
content: "";
display: block;
position: absolute;
left: 0;
bottom: 0;
height: 3px;
width: 0;
background-color: orange;
-webkit-transition: width 0.2s;
transition: width 0.2s;
}
.nav li:nth-last-of-type(1) a:after {
display: none;
}
.nav li a:hover:before {
width: 100%;
}
.nav li a:after {
content: "";
display: block;
position: absolute;
right: -8px;
top: 21px;
height: 6px;
width: 6px;
background: #ffffff;
opacity: .5;
}
.subnav {
list-style-type: none;
display: block;
position: relative;
top: -49px;
margin: 0;
z-index: 1;
background-color: #ccc;
-webkit-transition: top 0.2s;
}
.subnav li {
display: inline-block;
background-color: #ccc;
margin: 0 5px;
}
.subnav li a {
padding: 8px 10px;
font-size: 14px;
color: #EFEFEF;
display: block;
}
HTML
<div class="bodyframe div_container">
<header>
<nav>
<ul class="nav">
<li role="presentation" class="active">Home</li>
<li role="presentation">Services</li>
<li role="presentation">Products</li>
<li role="presentation">About</li>
<li role="presentation">Contact</li>
</ul>
</nav>
<ul class="subnav">
<li>Test</li>
<li>1243</li>
</ul>
</header>
</div>
If you only need the submenu to mimic the one in the example, without using jQuery, using the second chunk of HTML with the CSS you supplied you could do:
nav:hover~ul {
top: 0px;
}
This shows the next ul element, in this case the subnav, whenever the nav is hovered over ("~" selector means select the ul element preceded by nav:hover).
However, if you want to do something more dynamic... id suggest just using JS/jQuery as well

menu dropdown with extended list

I want to create smooth dropdown menu. The idea is: when clicking on orange element it will toggle black elements, and when clicking on black element it will toggle grey elements. But what i got is black element covering orange element with grey elements already toggled. I've used display:none; everywhere to make sure that it won't show up and.. Everything is toggled after clicking orange element. Using .hide() also doesn't want to help hide this elements. How can i make this black box (after click event) toggle below orange element and don't show grey? Also i don't know how to make grey element to not hide when someone will click on it.
$(".d").click(function(){
$(".d ul li").slideToggle(200);
});
$(".a").click(function(){
$(".b").slideToggle(200);
});
#container
{
width: 200px;
height: 500px;
background-color: rgba(0, 0, 0, 0.3);
overflow-y: scroll;
overflow-x: hidden;
}
ul
{
list-style-type: none;
margin: 0;
padding: 0;
}
.a, .c
{
position: relative;
display: none;
width: 200px;
height: 100px;
margin-bottom: 10px;
background-color: black;
cursor: pointer;
}
.b
{
position: relative;
display: none;
margin-bottom: 5px;
padding: 0;
height: 100px;
width: 200px;
background-color: rgba(0, 0, 0, 0.6);
}
.d, .e
{
font-size: 20px;
position: relative;
width: 200px;
height: 100px;
margin-bottom: 10px;
background-color: orange;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<div id="container">
<ul>
<li class="d">1
<ul>
<li class="a"></li>
<ul>
<li class="b"></li>
<li class="b"></li>
<li class="b"></li>
</ul>
<li class="c"></li>
<li class="c"></li>
</ul>
</li>
<li class="d">2</li>
</ul>
</div>
The page is getting over,so it can't scroll down so you should add element and can also use href="#id/.classname" inside element
for hide and then for looking page this can be done
$(document).ready(function(){
$(".a").hide();
$(".b").hide();
$(".d").click(function(){
$(".d").fadeIn('fast');
});
$(".a").click(function(){
$(".b").fadeIn('fast');
});

Long Navigation bar introducing horizontal scroll?

I have designed a Navbar in which the drop down are absolutely positioned with respect to their parent list items (Main menu list is position: relative and submenus position: absolute).
My navbar 's main list is long (stretches till right-end of page). So when I hover on the last main menu item, then my drop-down occupies it's width and the page introduces a horizontal scroll (goes outside the body content).
I want the dropdown's on extreme ends of the page to open from right to left so that they lie within the body itself and no scroll is introduced.
How can I achieve this??? Please Help.
I have designed a Navigation Bar as follows:
HTML:
<ul id="menu">
<li>Home</li>
<li>
Categories
<ul>
<li>CSS</li>
<li>Graphic design</li>
<li>Development tools</li>
<li>Web design</li>
</ul>
</li>
<li>Work</li>
<li>About</li>
<li>Contact</li>
</ul>
CSS:
#menu li {
float: left;
border-right: 1px solid black;
box-shadow: 1px 0 0 #444;
position: relative;
}
#menu ul {
position: absolute;
top: 38px;
left: 0;
margin: 20px 0 0 0;
opacity: 0;
visibility: hidden;
transition: all .2s ease-in-out;
}
#menu ul ul {
top: 0;
left: 195px;
margin: 0 0 0 20px;
}
#menu ul a {
padding: 10px;
width: 175px;
_height: 10px; /*IE6 only*/
display: block;
white-space: nowrap;
float: none;
text-transform: none;
}
#menu li:last-child ul {
left: auto;
right: 0;
}

Categories

Resources