I am currently developing a website and I have a problem for the responsive.
I have built my menu and I would like the sub-menus to appear after a hover when the screen width is greater than 980px
. Below this width, I would like that the user click on a menu before the sub-menus appear.
Here is my HTML code
<nav>
<ul class="menulist">
<li class="list-item"><a href="">list-item 1</a></li>
<li class="list-item"><a href="">list-item 2</a></li>
<li class="list-item"><a href="">list-item 3</a>
<a href="#"></a>
<ul class="sub-menu">
<li class="list-item"><a href="">list-item 4</a></li>
<li class="list-item"><a href="">list-item 5</a></li>
</ul>
</li>
</ul>
I tried to do it this way
if (window.matchMedia("(max-width: 980px)").matches) {
$('.list-item > .sub-menu').parent().click(function() {
var submenu = $(this).children('.sub-menu');
if ( $(submenu).is(':hidden') ) {
$(submenu).slideDown(200);
} else {
$(submenu).slideUp(200);
}
});
} else {
$('.list-item > .sub-menu').parent().hover(function() {
var submenu = $(this).children('.sub-menu');
if ( $(submenu).is(':hidden') ) {
$(submenu).slideDown(200);
} else {
$(submenu).slideUp(200);
}
});
}
And also in this way,
if ($(window).width() < 981) {
$('.list-item > .sub-menu').parent().click(function() {
var submenu = $(this).children('.sub-menu');
if ( $(submenu).is(':hidden') ) {
$(submenu).slideDown(200);
} else {
$(submenu).slideUp(200);
}
});
}
else {
$('.list-item > .sub-menu').parent().hover(function() {
var submenu = $(this).children('.sub-menu');
if ( $(submenu).is(':hidden') ) {
$(submenu).slideDown(200);
} else {
$(submenu).slideUp(200);
}
});
}
But it doesn't work.
In fact the behavior does not change when I resize my browser. When I load the site on a mobile (so width< 981), it works fine at first. I click on the links before the sub-menus open. But when I enlarge the browser, the behavior doesn't change. I have to click on the links again before the sub-menus open.
it's the same when I load on a wide screen. The hover works normally. And when I reduce the screen it doesn't let the click work, the hover continues to work.
Someone can help me, please? Thank you.