How can I delay the start of a CSS animation?

后端 未结 3 1532
-上瘾入骨i
-上瘾入骨i 2020-12-10 06:45

I\'m trying to delay the trigger of a CSS animation (not slow down the animation itself, but delay it a few seconds before starting). And the image should not display before

相关标签:
3条回答
  • 2020-12-10 07:05

    Add a settimeout function

    Hi there, you could add an event listen that get when you mouseover the certain element and then calls the function after 1 second.

    $('slideRight').on('mouseover',function(){
    
    window.setTimeout(function(){
        $this.addClass('onesecond');
    }, 1000); //<-- Delay in milliseconds
     });
    
    0 讨论(0)
  • 2020-12-10 07:16
    div {
        -webkit-animation-delay: 2s; /* Safari 4.0 - 8.0 */
        animation-delay: 2s;
    }
    

    Source:

    https://www.w3schools.com/cssref/css3_pr_animation-delay.asp https://developer.mozilla.org/en-US/docs/Web/CSS/animation-delay

    0 讨论(0)
  • 2020-12-10 07:19

    Delaying the start of the animation is very simple. Simply add the animation-delay property to your code:

    .slideRight{
        animation-name: slideRight;
        animation-duration: 1s;   
        animation-timing-function: ease-in-out;        
        visibility: visible !important;
        /* New code here: */
        animation-delay: 1s;
    }
    

    It's important to note that animation-delay only delays the start of the animation from the beginning. If you have a repeating animation, it won't add the delay to the same spot of each loop; only to the very beginning. There's currently no CSS property capable of that kind of looped delay.

    All major browsers currently support animation-delay without the need for vendor prefixes.


    As for your second question regarding the <a> element: Yes, it can work. The reason it's not working for you now is because <a> elements are inline elements. In order to make it work like you're expecting, add display: inline-block; to the .slideRight{} selector. Ultimately this is what your code will look like:

    .slideRight{
        animation-name: slideRight;
        animation-duration: 1s;   
        animation-timing-function: ease-in-out;     
        visibility: visible !important;
        /* New code here: */
        animation-delay: 1s;
        display: inline-block;
    }
    
    
    @keyframes slideRight {
        0% {
            transform: translateX(-150%);
        }
    
        100% {
            transform: translateX(0%);
        }   
    }
    <a class="slideRight">HI</a>

    JSFiddle Example

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