How to animate an element on hover

后端 未结 3 1405
一个人的身影
一个人的身影 2021-01-02 09:00

How can I make my

elements grow (and the content changes text size to a higher one), when hovered over? I put them in a class and tried:

<         


        
3条回答
  •  走了就别回头了
    2021-01-02 09:51

    Using CSS you can add a hover style to the div:

    div.container {
        width: 80%;
        background-color: blue;
    }
    
    div.container:hover {
        width: 100%;
        background-color: red;
    }
    

    See this jsFiddle for a demonstration.

    jQuery Solution

    Another option that might work for you is jQuery. It's a JavaScript library that simplifies commonly needed functionality such as this. Using jQuery, you can easily add hover effects to the elements:

    //hover effect applies to any elements using the 'container' class
    $(".container").hover(
        function(){ //mouse over
            $(this).width($(this).width() + 30);
        },
        function(){ //mouse out
            $(this).width($(this).width() - 30);
        }
    );
    

    See this jsFiddle for a demonstration.

提交回复
热议问题