How to create jQuery slide left and right toggle effect

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the jQuery animate() method

There are no such method like slideLeft() and slideRight() similar to slideUp() and slideDown(), but you can simulate these effects using jQuery animate() function.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="utf-8">
  • <title>jQuery slideLeft and slideRight Effect</title>
  • <style type="text/css">
  •     .box{
  •         float:left;
  •         overflow: hidden;
  •         background: #f0e68c;
  •     }
  •     /* Add padding and border to inner content
  •     for better animation effect */
  •     .box-inner{
  •         width: 400px;
  •         padding: 10px;
  •         border: 1px solid #a29415;
  •     }
  • </style>
  • <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
  • <script type="text/javascript">
  •     $(document).ready(function(){
  •         var boxWidth = $(".box").width();
  •         $(".slide-left").click(function(){
  •             $(".box").animate({
  •                 width: 0
  •             });
  •         });
  •         $(".slide-right").click(function(){
  •             $(".box").animate({
  •                 width: boxWidth
  •             });
  •         });
  •     });
  • </script>
  • </head>
  • <body>
  •     <button type="button" class="slide-left">Slide Left</button>
  •     <button type="button" class="slide-right">Slide Right</button>
  •     <hr>
  •     <div class="box">
  •         <div class="box-inner">Lorem ipsum dolor sit amet...</div>
  •     </div>
  • </body>
  • </html>

There is an even better way to create this effect. The following example will slide toggle the box from right-to-left and left-to-right something like slideToggle() effect.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="utf-8">
  • <title>jQuery Slide Left and Right Toggle Effect</title>
  • <style type="text/css">
  •     .box{
  •         float:left;
  •         overflow: hidden;
  •         background: #f0e68c;
  •     }
  •     /* Add padding and border to inner content
  •     for better animation effect */
  •     .box-inner{
  •         width: 400px;
  •         padding: 10px;
  •         border: 1px solid #a29415;
  •     }
  • </style>
  • <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
  • <script type="text/javascript">
  •     $(document).ready(function(){
  •         $(".slide-toggle").click(function(){
  •             $(".box").animate({
  •                 width: "toggle"
  •             });
  •         });
  •     });
  • </script>
  • </head>
  • <body>
  •     <button type="button" class="slide-toggle">Slide Toggle</button>
  •     <hr>
  •     <div class="box">
  •         <div class="box-inner">Lorem ipsum dolor sit amet...</div>
  •     </div>
  • </body>
  • </html>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.