How to prevent css keyframe animation to run on page load?

后端 未结 7 1851
轻奢々
轻奢々 2020-12-07 20:20

I have a div in which I animate the content:

相关标签:
7条回答
  • 2020-12-07 20:58

    Rotation animation that (appears) not to run until needed.
    The CSS below allows for up and down arrows for showing menu items. The animation does not appear to run on page load, but it really does.

    @keyframes rotateDown {
       from { transform: rotate(180deg); }
       to   { transform: rotate(0deg); }
    }
    
    @keyframes rotateUp {
       from { transform: rotate(180deg); }
       to   { transform: rotate(0deg); }
    }
    
    div.menu input[type='checkbox'] + label.menu::before {
       display            :inline-block;
       content            : "▼";
       color              : #b78369;
       opacity            : 0.5;
       font-size          : 1.2em;
    }
    
    div.menu input[type='checkbox']:checked + label.menu::before {
       display            : inline-block;
       content            : "▲";
       color              : #b78369;
       opacity            : 0.5;
       font-size          : 1.2em;
    }
    
    div.menu input[type='checkbox'] + label.menu {
       display            : inline-block;
       animation-name     : rotateDown;
       animation-duration : 1ms;
    }
    
    div.menu input[type='checkbox']:checked + label.menu {
       display            : inline-block;
       animation-name     : rotateUp;
       animation-duration : 1ms;
    }
    
    div.menu input[type='checkbox'] + label.menu:hover {
       animation-duration : 500ms;
    }
    
    div.menu input[type='checkbox']:checked + label.menu:hover {
       animation-duration : 500ms;
    }
    

    From top to bottom:

    1. Create the rotations. For this there are two... one for the down arrow and one for the up arrow. Two arrows are needed, because, after the rotation, they return to their natural state. So, the down arrow starts up and rotates down, while the up arrow starts down and rotates up.
    2. Create the little arrows. This is a straight forward implementation of ::before
    3. We put the animation on the label. There is nothing special, there, except that the animation duration is 1ms.
    4. The mouse drives the animation speed. When the mouse hovers over the element, the animation-duration is set to enough time to seem smooth.

    Working on my site

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