Näytä Hover menu >>
Toggle Sub Navs
Alavalikko avautuu kun linkkiä on painettu
HTML
CSS
#nav ul{
display: flex;
list-style-type: none;
padding: 0;
}
#nav ul li a, #nav ul li label{
display: block;
background-color: rgb(48, 48, 48);
padding: 1em 2em;
position: relative;
}
#nav ul li ul{
display: none;
position: absolute;
}
#nav input[type="radio"]{
display: none;
}
#nav input[type="radio"]:checked ~ ul{
display: block;
}
#nav a, #nav a:visited, #nav label{
color: white;
text-decoration: underline;
}
#nav a:hover, #nav label:hover{
color: orange;
cursor: pointer;
}
JS
const radios = document.querySelectorAll("#nav input[type='radio']");
const close_menus = () => {
for (let checkbox of radios) {
checkbox.checked = false;
}
let $radios = $('#nav input[type="radio"]');
$radios.prop('checked', false).data('checked', false);
}
const setup = () => {
const nav_sub_items = document.querySelectorAll("#nav ul li ul li *")
nav_sub_items.forEach((element) => {
element.addEventListener('click', () => {close_menus()});
})
const nav_items = document.querySelectorAll("#nav ul li a")
nav_items.forEach((element) => {
element.addEventListener('click', () => {close_menus()});
})
let $radios = $('#nav input[type="radio"]');
$radios.click(function () {
let $this = $(this);
if ($this.data('checked')) {
this.checked = false;
}
let $otherRadios = $radios.not($this).filter('[name="'+ $this.attr('name') + '"]');
$otherRadios.prop('checked', false).data('checked', false);
$this.data('checked', this.checked);
});
}
setup()