Home Entrance Everyone Wiki Search Login Register

Welcome, Guest. Please login or register. - Thinking of joining the forum??
April 20, 2024 - @109.07 (what is this?)
Forum activity rating: Three Star Posts: 44/1k.beats Unread Topics | Unread Replies | Own Posts | Own Topics | Random Topic | Recent Posts
News: :4u: ~~~~~~~~~~~  :4u:

+  MelonLand Forum
|-+  World Wild Web
| |-+  ✁ ∙ Web Crafting
| | |-+  ☔︎ ∙ I need Help!
| | | |-+  Triggering a CSS animation using js and a button?


« previous next »
Pages: [1] Print
Author Topic: Triggering a CSS animation using js and a button?  (Read 82 times)
Icey!
Sr. Member ⚓︎
****



View Profile WWW

First 1000 Members!Pro Bug Finder!OG! Joined 2021!High Speed Ozwomp!
« on: April 11, 2024 @26.06 »

I've been trying to make a page where if you press a button it triggers an animation but it seems that it will only play once and I can't seem to figure out a way to reset it so I can play it again.

Code
<!DOCTYPE html>
<html>
  <head>
  <style>
    body{
        background: pink;
    }
    #object {
        background: cyan;
        height: 100px;
        width: 100px;
        text-align: center;
    }
    @keyframes spin {
        from { 
            rotate: 0deg;
        }
        to {
            rotate: 360deg;
        }
    }
  </style>
    </head>
  <body>
    <div id="object"><p>-w-</p></div>
    <button onclick="playanim()" id="button">play</button>
  <script type="text/javascript">
        var obj = document.getElementById("object")

        function playanim() {
          obj.style.animation = null // <-- I want to reset the animation when pressing the button to play it again.
          obj.style.animation = "spin 2s"
        }
  </script>
  </body>
</html>

Anything helps! Thank you.
Logged



:ozwomp: my beloved

lilac
Casual Poster
*


Just for Fun. No, Really.


View Profile

Story Builder!First 1000 Members!Joined 2023!
« Reply #1 on: April 11, 2024 @401.20 »

Seems like this is the easiest way to do it. Apparently it has something to do with reflow

Code
obj.style.animation = null; // <-- I want to reset the animation when pressing the button to play it again.
obj.offsetHeight; // <-- Add this line
obj.style.animation = "spin 2s";
Logged

Icey!
Sr. Member ⚓︎
****



View Profile WWW

First 1000 Members!Pro Bug Finder!OG! Joined 2021!High Speed Ozwomp!
« Reply #2 on: April 11, 2024 @829.99 »

Seems like this is the easiest way to do it. Apparently it has something to do with reflow

Code
obj.style.animation = null; // <-- I want to reset the animation when pressing the button to play it again.
obj.offsetHeight; // <-- Add this line
obj.style.animation = "spin 2s";

works thamk u!!!!! :D
Logged



:ozwomp: my beloved

Pages: [1] Print 
« previous next »
 

Vaguely similar topics! (3)

HELP: Custom cursor that changes when you are pressing the mouse button

Started by Lulu FinksBoard ☔︎ ∙ I need Help!

Replies: 4
Views: 627
Last post January 29, 2023 @1.56
by Lulu Finks
Funny Video Game Animations! (SFM, MMD, GMod, etc.)

Started by WingsOfImaginationBoard ♖ ∙ Video Games

Replies: 9
Views: 746
Last post January 14, 2024 @897.75
by alexela64
A cool avatar site (andypants.net)

Started by SilkSkullBoard ⚛︎ ∙ Share your Resources

Replies: 17
Views: 1547
Last post April 01, 2023 @105.91
by SilkSkull

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021, Simple Machines | Terms and Policies Forum Guide | Rules | RSS | WAP2


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi