Code has been added to clipboard!

Alternating Between Slide Animation Methods With jQuery .slideToggle()

Reading time 1 min
Published Jan 16, 2018
Updated Oct 2, 2019

jQuery slideToggle: Main Tips

  • The .slideToggle() method alternates between the slideUp() and slideDown() methods each time it is applied.
  • This method can be used to animate the height of any element (i.e., an image).
  • jQuery toggle slide belongs to the group of slide effects.

How to Toggle Slide Animations

The jQuery .slideToggle() method alternates between the .slideUp() and .slideDown() methods each time it is applied. In the example below, you can use the same button to both show and hide an image with slide animations:

Example
$("button").click(() => {
    $("img").slideToggle();
});

To toggle slide animations, follow these syntax rules:

$("selector").slideToggle(length, easing, callback);

jQuery .slideToggle() accepts three arguments:

  • length - a numeric value (400 by default), indicating how many milliseconds that the animation lasts. Keywords can be used as well: slow (600 ms) and fast (200 ms).
  • easing - a keyword (either linear or swing), indicating the changes in the animation speed.
  • callback - the name of the function that will run after the animation.
Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX