Home Events! Entrance Everyone Wiki Search Login Register

Welcome, Guest. Please login or register. - Thinking of joining the forum??
November 24, 2024 - @219.76 (what is this?)
Forum activity rating: Three Stars Posts: 45/1k.beats ~ Boop! The forum will close in 781.beats! Unread Topics | Unread Replies | My Stuff | Random Topic | Recent Posts    Start New Topic
News: :skull: Websites are like whispers in the night  :skull:

+  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 633 times)
Icey!
Sr. Member ⚓︎
****


Icey/Iceys

⛺︎ My Room

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.

⛺︎ My Room

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 ⚓︎
****


Icey/Iceys

⛺︎ My Room

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: 1000
Last post January 29, 2023 @1.56
by Lulu Finks
Is the "Download Site" button from NeoCities' Dashboard missing for anyone else?

Started by SquaredPancakeBoard ✁ ∙ Web Crafting

Replies: 15
Views: 1843
Last post September 26, 2023 @680.31
by Kolo
General Animation Thread ?

Started by BadgersaurusBoard ⛺︎ ∙ Cinema

Replies: 8
Views: 1283
Last post July 07, 2023 @941.06
by Corrupted Unicorn

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


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