jQuery animate() animate div height on mouse hover

Introduction

Use the jQuery animate() method in combination with the mouseenter() and mouseleave() methods.

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Animate Div Height on Hover in jQuery</title>
<style>
    .box{/*from  www .j  a  va2  s  .  co m*/
        width: 400px;
        height: 150px;
        background: #f0e68c;
        border: 1px solid #a29415;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function(){
        var boxHeight = $(".box").height();
        $(".box").mouseenter(function(){
            $(this).animate({
                height: "300"
            });
        }).mouseleave(function(){
            $(this).animate({
                height: boxHeight
            });
        });
    });
</script>
</head>
<body>
    <div class="box">
    mouse pointer over the box to play the animation
    </div>
</body>
</html>



PreviousNext

Related