CSS how to make an element fade in and then fade out?

后端 未结 5 580
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 20:11

I can make an element with an opacity of zero fade in by changing its class to .elementToFadeInAndOut with the following css:

.elementToFadeInAndOut {
    op         


        
5条回答
  •  醉话见心
    2020-11-29 20:47

    If you need a single fadeIn/Out without an explicit user action (like a mouseover/mouseout) you may use a CSS3 animation: http://codepen.io/anon/pen/bdEpwW

    .elementToFadeInAndOut {
    
        animation: fadeinout 4s linear 1 forwards;
    }
    
    
    
    @keyframes fadeinout {
      0% { opacity: 0; }
      50% { opacity: 1; }
      100% { opacity: 0; }
    }
    

    By setting animation-fill-mode: forwards the animation will retain its last keyframe

    By setting animation-iteration-count: 1 the animation will run just once (change this value if you need to repeat the effect more than once)

提交回复
热议问题