How to animate div height on mouse hover using jQuery

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the jQuery animate() method

You can use the jQuery animate() method in combination with the mouseenter() and mouseleave() methods to animate the height of a <div> element on mouseover.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="utf-8">
  • <title>jQuery Animate Div Height on Hover</title>
  • <style type="text/css">
  •     .box{
  •         width: 400px;
  •         height: 150px;
  •         background: #f0e68c;
  •         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 boxHeight = $(".box").height();
  •         $(".box").mouseenter(function(){
  •             $(this).animate({
  •                 height: "300"
  •             });
  •         }).mouseleave(function(){
  •             $(this).animate({
  •                 height: boxHeight
  •             });
  •         });
  •     });
  • </script>
  • </head>
  • <body>
  •     <p><strong>Note:</strong> Place mouse pointer over the box to play the animation.</p>
  •     <div class="box"></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.