Home  >  Article  >  Web Front-end  >  Cleverly use the CSS steps() function to achieve a random card flip effect!

Cleverly use the CSS steps() function to achieve a random card flip effect!

青灯夜游
青灯夜游forward
2021-07-01 11:29:061942browse

Cleverly use the CSS steps() function to achieve a random card flip effect!

Flop is an interactive effect that everyone is familiar with, and it usually appears in lottery activities. So is it possible to achieve a random card flop effect without JavaScript? There is definitely no problem with the card flop effect. CSS does not have random functions. Today I will share an alternative interactive implementation idea.

CSS There is no built-in "random" function, there is no Math.random() function like Javascript, and there is no Unable to generate random numbers or random colors.

Based on the above problem, it is necessary to change the idea and let the elements achieve random effects through complex animation. The implementation principle allows the cards to quickly show different states, allowing these cards to cycle through all 52 states within 1 second. The user clicks on each card to pause the animation and let the cards flip.

Cleverly use the CSS steps() function to achieve a random card flip effect!

Online preview: https://codepen.io/quintiontang/pen/OJmJRrV

The shortcoming of this plan is that it cannot avoid three pictures Cards appear of the same suit and face

Summary

The idea of ​​using animation to make elements appear to behave randomly is not very interesting and a bit unexpected , most users will definitely not realize that this effect is achieved by pure CSS.

Now CSS is far more than what we see now. What it can bring depends on creativity. I have always believed that there are no interactions that cannot be achieved, only interactions that are unexpected.

Key points

The effect mainly uses the animation properties in CSSanimation, to customize an animation processrandomAnim, click Through the classic input checkbox label combination, animation controller animation-play-state: paused and animation-play-state: running;, the following are the key steps().

steps() is a step function (timing-function) that allows animation or transition effects to be divided into segments instead of continuing from one state to Transition to another state. This function has two parameters:

  • The first parameter is a positive value that specifies the number of segments I want the animation to be divided into.
  • The second parameter is optional and can be set to the following values: start and end, indicating that a step change occurs at the starting point or end point of each interval. The default is end. For example, steps(1,start), the animation is divided into 1 step, and the left endpoint is the start when the animation is executed; steps(1,end), the animation is divided into 1 step, and the animation When executed, the part that is the end endpoint on the right side is the beginning.

Cleverly use the CSS steps() function to achieve a random card flip effect!

The step function (timing-function) is used between each two key frames, not the entire animation .

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Cleverly use the CSS steps() function to achieve a random card flip effect!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete