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

后端 未结 5 563
没有蜡笔的小新
没有蜡笔的小新 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)

    0 讨论(0)
  • 2020-11-29 20:48

    I found this link to be useful: css-tricks fade-in fade-out css.

    Here's a summary of the csstricks post:

    CSS classes:

    .m-fadeOut {
      visibility: hidden;
      opacity: 0;
      transition: visibility 0s linear 300ms, opacity 300ms;
    }
    .m-fadeIn {
      visibility: visible;
      opacity: 1;
      transition: visibility 0s linear 0s, opacity 300ms;
    }
    

    In React:

    toggle(){
        if(true condition){
            this.setState({toggleClass: "m-fadeIn"});
        }else{
            this.setState({toggleClass: "m-fadeOut"});
        }
    }
    
    render(){
        return (<div className={this.state.toggleClass}>Element to be toggled</div>)
    }
    
    0 讨论(0)
  • 2020-11-29 20:49

    Use css @keyframes

    .elementToFadeInAndOut {
        opacity: 1;
        animation: fade 2s linear;
    }
    
    
    @keyframes fade {
      0%,100% { opacity: 0 }
      50% { opacity: 1 }
    }
    

    here is a DEMO

    .elementToFadeInAndOut {
        width:200px;
        height: 200px;
        background: red;
        -webkit-animation: fadeinout 4s linear forwards;
        animation: fadeinout 4s linear forwards;
    }
    
    @-webkit-keyframes fadeinout {
      0%,100% { opacity: 0; }
      50% { opacity: 1; }
    }
    
    @keyframes fadeinout {
      0%,100% { opacity: 0; }
      50% { opacity: 1; }
    }
    <div class=elementToFadeInAndOut></div>

    Reading: Using CSS animations

    You can clean the code by doing this:

    .elementToFadeInAndOut {
        width:200px;
        height: 200px;
        background: red;
        -webkit-animation: fadeinout 4s linear forwards;
        animation: fadeinout 4s linear forwards;
        opacity: 0;
    }
    
    @-webkit-keyframes fadeinout {
      50% { opacity: 1; }
    }
    
    @keyframes fadeinout {
      50% { opacity: 1; }
    }
    <div class=elementToFadeInAndOut></div>

    0 讨论(0)
  • 2020-11-29 20:57

    Try this:

    @keyframes animationName {
      0%   { opacity:0; }
      50%  { opacity:1; }
      100% { opacity:0; }
    }
    @-o-keyframes animationName{
      0%   { opacity:0; }
      50%  { opacity:1; }
      100% { opacity:0; }
    }
    @-moz-keyframes animationName{
      0%   { opacity:0; }
      50%  { opacity:1; }
      100% { opacity:0; }
    }
    @-webkit-keyframes animationName{
      0%   { opacity:0; }
      50%  { opacity:1; }
      100% { opacity:0; }
    }   
    .elementToFadeInAndOut {
       -webkit-animation: animationName 5s infinite;
       -moz-animation: animationName 5s infinite;
       -o-animation: animationName 5s infinite;
        animation: animationName 5s infinite;
    }
    
    0 讨论(0)
  • 2020-11-29 20:58

    A way to do this would be to set the color of the element to black, and then fade to the color of the background like this:

    <style> 
    p {
    animation-name: example;
    animation-duration: 2s;
    }
    
    @keyframes example {
    from {color:black;}
    to {color:white;}
    }
    </style>
    <p>I am FADING!</p>
    

    I hope this is what you needed!

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