WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

How to animate div width on mouse hover using jQuery

Topic: JavaScript / jQueryPrev|Next

Answer: Use the jQuery animate() method

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

Let's take a look at an example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Animate Div Width on Hover</title>
<style>
    .box{
        width: 200px;
        height: 150px;
        background: #f0e68c;
        border: 1px solid #a29415;
    }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
    $(document).ready(function(){
        var boxWidth = $(".box").width();
        $(".box").mouseenter(function(){
            $(this).animate({
                width: "400"
            });
        }).mouseleave(function(){
            $(this).animate({
                width: boxWidth
            });
        });
    });
</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:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties