In jQuery, I know that we can pass true
or false
value to the function to toggle
visibility of an element. I can't get slideToggle
to do the same thing. Any help please?
For example, instead of:
if (true)
$('something').slideDown();
else
$('something').slideUp();
I would really like to do something similar to:
$('something').toggle(myBoolValue);
But I want to do it with slideToggle
.
Look at my example of jsfiddle to know what I am trying to do.
Is that possible?
slideToggle() method animates the height of the matched elements. This causes lower parts of the page to slide up or down, appearing to reveal or conceal the items. If the element is initially displayed, it will be hidden; if hidden, it will be shown. The display property is saved and restored as needed.
The slideToggle() method toggles between slideUp() and slideDown() for the selected elements. This method checks the selected elements for visibility. slideDown() is run if an element is hidden. slideUp() is run if an element is visible - This creates a toggle effect.
slideToggle
optionally takes 2 parameters duration
and a callback
so passing true/false to show/hide will not work. You might have to write your own plugin which will implement this logic. Something like this
$.fn.mySllideToggle = function(show){
if(show){
$(this).slideDown();
}
else{
$(this).slideUp();
}
}
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