Resume and pause animations in CSS
source link: https://www.amitmerchant.com/run-and-pause-animations-in-css/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Resume and pause animations in CSS
July 29, 2023 · CSS
I have been working with CSS for like 10+ years now and I have worked with CSS animations for a fair amount of time. But I didn’t know that you can pause and resume animations in CSS until recently.
Essentially, you can pause and resume animations in CSS using the animation-play-state
property. This property accepts two values: running
and paused
.
The running
value is the default value and it lets the animation run as normal. The paused
value pauses the animation.
This can help you build some interesting UIs. For instance, you can pause the animation when the user hovers over the element and resume it when the user hovers out of the element.
Or turn it off on a button click and turn it on again when the user clicks the button again.
Here’s an example of the same.
.container {
display: flex;
height: 100%;
background: linear-gradient(
to right,
#7953cd 20%,
#00affa 30%,
#0190cd 70%,
#764ada 80%
);
background-size: 500% auto;
animation: animatedGradient 3s ease-in-out infinite;
animation-play-state: running;
}
.container:hover {
animation-play-state: paused;
}
@keyframes animatedGradient {
0% {
background-position: 0% 50%;
}
100% {
background-position: 100% 50%;
}
}
As you can tell, the animatedGradient
animation will be paused when the user hovers over the .container
element and resumes when the user hovers out of the element.
Here’s a CodePen to play with.
Previous: An introduction to Laravel Folio Next: Mixing colors to create variants in CSS
👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!
Comments?
Recommend
-
6
CSS Animations Level 1↑ → CSS Animations Level 1 Editor’s Draft, 18 September 2020...
-
6
Additive Animations in CSS October 3, 2020 The following is supported in the latest Firefox, Edge, and Chrome (and also Safari Technology Preview). Yay. What happens when you have two simultaneous animat...
-
15
CSS + the Web Animations API April 15, 2020 As of this writing, the features discussed are in the latest stable versions of Firefox (75) and Safari (version 13.1, iOS 13.4). They are also in Edge and Chrome with...
-
11
Simple CSS Line Hover Animations for Links A couple of simple & subtle CSS-based line hover animations for links. ...
-
10
Ideas for CSS Button Hover Animations Some inspiration for button hover animations using CSS only. By...
-
5
Learning CSS by Styling a ResumeOnce in a while, I make it a practice to hack my resume. The goal isn't explicitly to keep it relevant, though. Instead, I use it as an excuse to practice CSS. (B...
-
3
Pause and resume a JavaScript function JavaScriptReact In JavaScript, IO (eg Filesystem IO, Network IO) is asynchronous. That means when you are calling a function that involves IO, you got to have a callback functi...
-
3
WhatsApp is improving voice messages with out of chat playback, pause/resume recording Today WhatsApp has announced it's working to...
-
2
Frontend Focus Issue 584 « Prev
-
4
Christmas Tree Animations Made With CSS and JS Posted in HTML & CSS With Christmas just around t...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK