MouseOver and MouseOut In CSS

前端 未结 4 1433
有刺的猬
有刺的猬 2020-12-23 17:40

For using mouse into one element we use the :hover CSS attribute. How about for mouse out of the element?

I added a transition effect on the element to

相关标签:
4条回答
  • 2020-12-23 18:15

    I think that I've found the solution.

    .class :hover {
        /*add your animation of mouse enter*/
    }
    
    .class {
        /*
        no need for not(hover) or something else. 
        Just write your animation here and it will work when mouse out
        */
    }
    

    Just try it... :)

    0 讨论(0)
  • 2020-12-23 18:26

    Here is the best solution, i think.

    CSS onomouseout:

    div:not( :hover ){ ... }
    

    CSS onmouseover:

    div:hover{ ... }
    

    It's better, because if you need to set some styles ONLY onmouseout and trying to do this in this way

    div { ... }
    

    you will set your styles and for onmouseover too.

    0 讨论(0)
  • 2020-12-23 18:29

    CSS itself does not support a mousein or mouseout selector.

    The :hover selector will apply to the element while the mouse is over it, adding the style when the mouse enters and removing the style when the mouse leaves.

    The nearest approach is to define the styles which you would place in mouseout within your default (non-hover) styles. When you mouse-over the element the styles within hover will take effect, emulating a mousein, and when you move your mouse off the element the default styles will take effect again, emulating mouseout.

    Here is an example, taken from here:

    div {
        background: #2e9ec7;
        color: #fff;
        margin: 0 auto;
        padding: 100px 0; 
        -webkit-transition: -webkit-border-radius 0.5s ease-in;
        -moz-transition: -moz-border-radius 0.5s ease-in;
        -o-transition: border-radius 0.5s ease-in;
        -ms-transition: border-radius 0.5s ease-in;
        transition: border-radius 0.5s ease-in;
        text-align: center;
        width: 200px;
    }
    
    
    div:hover {
        background: #2fa832;
        -webkit-border-radius: 100px;
        -moz-border-radius: 100px;
        border-radius: 100px;
        -webkit-transition: all 1s ease;
        -moz-transition: all 1s ease;
        -o-transition: all 1s ease;
        -ms-transition: all 1s ease;
        transition: all 1s ease;
        -webkit-transform: rotate(720deg);
        -moz-transform: rotate(720deg);
        -o-transform: rotate(720deg);
        -ms-transform: rotate(720deg);
        transform: rotate(720deg);
    }
    

    The transitions defined for the div:hover style will take effect when the mouse enters (and hover is applied). The transitions for the div style will take effect when the mouse leaves (and hover is removed). This results in the mousein and mouseout transitions being different.

    0 讨论(0)
  • 2020-12-23 18:30

    You only need the :hover , when you mouse out of the element, it'll return to it's default non-:hover state, like this:

    .class { color: black; } 
    .class:hover { color: red; }
    

    when you hover, the color will be red and when you "mouseout", the color will return to black because it no longer matches the :hover selector. This is the default behavior for all browsers, nothing special you need to do here.

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