Pure CSS Implementation
Below we explore, the use of pure CSS can be implemented.
Hover pseudo-class implementation
Use the hover pseudo-class to control the pause of the animation style when the mouse hovers over the button.
The key code is as follows:
<div class= "btn Stop" >stop</div>
<div class= "Animation" ></div>
<style>
. stop:hover ~. Animation {
animation-play-state:paused;
}
</style>
Demo--Pure CSS to achieve the pause and play of CSS Animation (Hover):
Of course, this method is not smart enough, if you release the freedom of the mouse, click on the pause, and then click on the play is good. Is there any other way?
Checked pseudo-class implementation
Previous article "Talk about some interesting CSS topics (eight)--Pure CSS Navigation bar tab switching Scheme" also talked about, using radio tag checked pseudo class, plus <label for> implement pure CSS capture click things.
And you can control some CSS styles with the clicked element. The implementation is as follows:
<input id= "Stop" type= "Radio" name= "Playanimation"/>
<input id= "Play" type= "Radio" name= "Playanimation"/>
<div class= "box" >
<label for= "Stop" >
<div class= "BTN" >stop</div>
</label>
<label for= "Play" >
<div class= "BTN" >play</div>
</label>
</div>
<div class= "Animation" ></div>
Some key CSS code:
. Animation {
animation:move 2s linear infinite alternate;
}
#stop: Checked ~. Animation {
animation-play-state:paused;
}
#play: Checked ~. Animation {
animation-play-state:running;
}
We hope that when #stop and #play two radio are clicked, the. Animation elements are given animation-play-state:paused or Animation-play-state:running respectively. And they can only take effect one, so you need to give two radio tags the same name attribute.
CSS Code implementation