Is it best practice to check if an element is visible before hiding it, or is ok just to hide it? What are the benefits either way?
In the example below, the first checks for visibility before hiding the .foo
element...
$('.dropdown').hover(function(){
$(this).find('.menu').fadeToggle();
if($('.foo').is(':visible')){
$('.foo').fadeOut();
};
});
or
$('.dropdown').hover(function(){
$(this).find('.menu').fadeToggle();
$('.foo').fadeOut();
});
This is a note from jQuery's .fadeOut() specs:
Note: To avoid unnecessary DOM manipulation, .fadeOut() will not hide an element that is already considered hidden.
So, relax... There's nothing wrong with hiding an element that's already hidden. JQuery will check it for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With