The method .hover
fires 2 events: mouseenter
and mouseleave
. When using only 1 function in the method, you run the same code (.slideToggle()
) 2 times, which causes bug, because it will line up the animation where one will run over the other.
Instead, treat events within the .hover
separately, each with its own function:
$('#showDropdown').hover(
function(){
// AQUI TRATA O MOUSEENTER
},
function(){
// AQUI TRATA O MOUSELEAVE
}
);
Instead of using .slideToggle()
, use .slideDown()
in the mouseenter
and .slideUp()
in the mouseleave
.
And to avoid even more bugs, add a if
in the first function checking if the element is invisible before applying the animation that shows the element.
Your code would look like this:
$(document).ready(function() {
$('#showDropdown').hover(
function() {
if(!$('.dropdown-areas').is(":visible"))
$('.dropdown-areas').slideDown(1000);
},
function() {
$('.dropdown-areas').slideUp(1000);
}
);
});
.dropdown-areas {
text-align: left;
background-color: rgba(54, 52, 53, 0.9);
padding: 10px 0;
width: 100%;
display: none;
position: absolute;
}
.dropdown-areas li {
padding: 10px 0 10px 20px;
width: 100%;
list-style-type: none;
}
.dropdown-areas li a {
border-left: 3px solid var(--corFont);
padding-left: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="showDropdown" <?=$page=='areas.php' ? 'class="active"' : '';?>>
<a class="areas" href="Javascript:void(0);">ÁREAS DE ATUAÇÃO</a>
<ul class="dropdown-areas">
<? while($row_rsAreas = mysql_fetch_assoc($rsArea)) { ?>
<li>
<a href="area.php?id=<?=$row_rsAreas['area_id'];?>"><?=$row_rsAreas['area_titulo'];?></a>
</li>
<? } ?>
</ul>
</li>
You can also use the method .stop() in each function before applying the slide. This dispenses with using the if
in the first job:
$(document).ready(function() {
$('#showDropdown').hover(
function() {
$('.dropdown-areas').stop().slideDown(1000);
},
function() {
$('.dropdown-areas').stop().slideUp(1000);
}
);
});
.dropdown-areas {
text-align: left;
background-color: rgba(54, 52, 53, 0.9);
padding: 10px 0;
width: 100%;
display: none;
position: absolute;
}
.dropdown-areas li {
padding: 10px 0 10px 20px;
width: 100%;
list-style-type: none;
}
.dropdown-areas li a {
border-left: 3px solid var(--corFont);
padding-left: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="showDropdown" <?=$page=='areas.php' ? 'class="active"' : '';?>>
<a class="areas" href="Javascript:void(0);">ÁREAS DE ATUAÇÃO</a>
<ul class="dropdown-areas">
<? while($row_rsAreas = mysql_fetch_assoc($rsArea)) { ?>
<li>
<a href="area.php?id=<?=$row_rsAreas['area_id'];?>"><?=$row_rsAreas['area_titulo'];?></a>
</li>
<? } ?>
</ul>
</li>
Operating on mobile devices (touch screen)
How these devices do not detect mouseenter
, just add an event .click
in the element, this time with .slideToggle()
:
$('#showDropdown').click(function(){
$('.dropdown-areas').slideToggle(1000);
});
Dude I think you’ll have to put a setTimeout() there to prevent the animation from starting again before you finish or stop if the user removes or goes in and out of the menu something like this... I don’t know jQuery so I couldn’t help you much in this
– hugocsl