Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active September 22, 2017 23:12
Show Gist options
  • Save vielhuber/dee82dc612716378dae7 to your computer and use it in GitHub Desktop.
Save vielhuber/dee82dc612716378dae7 to your computer and use it in GitHub Desktop.
Dropdown-Menu with delayed hover effect #js #css #html
<div class="menu">
<a href="#">Menüpunkt</a>
<ul>
<li>
<a href="#">Untermenüpunkt</a>
<ul>
<li>Unteruntermenüpunkt</li>
<li>Unteruntermenüpunkt</li>
<li>Unteruntermenüpunkt</li>
</ul>
</li>
<li>
<a href="#">Untermenüpunkt</a>
<ul>
<li>Unteruntermenüpunkt</li>
<li>Unteruntermenüpunkt</li>
<li>Unteruntermenüpunkt</li>
</ul>
</li>
<li>
<a href="#">Untermenüpunkt</a>
<ul>
<li>Unteruntermenüpunkt</li>
<li>Unteruntermenüpunkt</li>
<li>Unteruntermenüpunkt</li>
</ul>
</li>
</ul>
</div>
$(document).ready(function() {
$( ".menu > ul > li" ).hover(function() {
var self = this;
window.setTimeout(function() {
if( $(self).hasClass('hover-init') ) {
$('.menu .hover').removeClass('hover');
$(self).addClass('hover');
}
},1000);
$(this).addClass('hover-init');
}, function() {
var self = this;
window.setTimeout(function() {
if( $(self).hasClass('hover') && !$(self).hasClass('hover-init') ) { $(self).removeClass('hover'); }
},1000);
$(this).removeClass('hover-init');
});
});
.menu {
width:200px;
}
.menu > ul > li {
border:1px solid red;
position:relative;
}
.menu > ul > li > ul {
display:none;
position:absolute;
top:0;
left: 100%;
width:200px;
border:1px solid blue;
}
.menu > ul > li > ul.hover,
.menu > ul > li.hover > ul {
display:block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment