How to animate an element on hover

后端 未结 3 1404
一个人的身影
一个人的身影 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:40

    I did something like this for a similar problem (you can change the scale to whatever works for you):

    div:hover {
     -webkit-transform: scale(1.1);
     -moz-transform:    scale(1.1);
     -o-transform:      scale(1.1);
     -ms-transform:     scale(1.1);
    }
    

    Note that this will scale both the div and its content, which I think is what you want.

    0 讨论(0)
  • 2021-01-02 09:50

    CSS3 solution:

    div {
        background: #999;
        width: 200px;
        height: 20px; 
        transition: width 1s;
    }
    
    div:hover{
        width: 300px;
    }
    <div>
        <p>Im content</p>
    </div>

    http://jsfiddle.net/MrdvW/

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题