I am looking to utilize a single button in Javascript to control the pausing and playing of a CSS animation, while also adjusting both the sun and sky colors

HTML CODE:
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset = "utf-8">
        <title>Keyframe Animations</title>
        <link rel="stylesheet" href="SunLabcss.css" type="text/css"/>
        <script src="scriptSun.js"></script>


    </head>
    <body>
        <button id = "sunButton" onclick="sunPausePlayToggle()">PLAY/PAUSE</button

sunPausePlayToggle() method in JS will manage the animation .

        <div id = "sunSky"

this div serves as a sky background

            <div id = "sun"></div

the upper div is a sun that rises from right and sets on the left

        </div>
    </body>
</html>


CSS code:

I am using keyframe animations to animate the sun and sky.

@keyframes risesky {
    0% {
        top: 100%;
        left : 100%;
        background-color: red;
    }
    50% {
        left:50%;
        top: 10%;
        background-color: yellow;

    }
    100% {
        background-color: orangered;
        top: 100%;
        left: 0%;
    }
}

@keyframes sky {
    0% , 100% {
        background-color: #1c1341;

    }
    10% {
        background-color: darkorange;
    }
    50% {
        background-color: skyblue;

    }
    80% {
        background-color: crimson;

    }
}

#sunButton{
    animation-play-state: running;
}

#sun {
    position: relative;
    border-radius: 50%;
    width: 80px;
    height: 80px;
    animation: risesky 11s infinite;

}

#sunSky {
    height: 500px;
    overflow: hidden;
    animation:  sky 11s infinite both;
    
}


Javascript code:

Here's the issue I'm facing, I can only pause the animation in JS, but cannot play it back.

function sunPausePlayToggle(){
    document.getElementById("sunSky").style.animationPlayState = "paused";
    document.getElementById("sun").style.animationPlayState = "running";
    
}

Answer №1

How to pause and play the sun animation using CSS

Here is the Javascript code snippet:

function toggleSunAnimation(){
    let sunsky = document.getElementById("sunSky")
    let sun = document.getElementById("sun")
    
    const skyState = sunsky.style.animationPlayState === 'running';
    const sunState = sun.style.animationPlayState === 'running';

    sunsky.style.animationPlayState = skyState ? 'paused' : 'running';
    sun.style.animationPlayState = sunState ? 'paused' : 'running';
    
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for creating a basic 2D grid using PaperJS without interactive features

I have developed an interactive application mockup using PaperJS, but there is a small feature missing. I am looking to create a 2D grid for user interactions when dragging objects on the screen. The grid should be static and act as guides for users. Howe ...

Loading a three.js model with OBJMTLLoader and spinning it around

After successfully using the OBJMTLLoader class for one obj file and achieving proper rotation around a fixed point on the object with object.rotation.y -= 0.5, I encountered an unexpected issue when trying to replicate the same code with a different .obj ...

Conceal the content in order to determine the height of images displayed next to each

I am working on a layout where there are rows with images in the left column and text in the right column. The images have a ratio of 16:9. In case the text becomes too long, I would like to show only a portion of it with a gradient mask and provide a "Rea ...

Avoiding HTML code within XML

I'm currently experiencing an issue with an invalid XML character appearing during interaction with a SOAP web service. When sending a formatted HTML message to a specific web service, I encountered a failed message that read as follows: Fault messa ...

I'm having trouble with the live sass compiler plugin in vs code - it's not syncing up with my css file properly

click here for image description I am facing an issue where my CSS file is not translating the codes in my SCSS file properly and adding unwanted comment lines. If anyone has insights on why this might be happening, I would greatly appreciate it. ...

Getting the Response Header location in jQuery using the $.get() method

My goal is to extract the location from a header response using jQuery's get method. I have attempted to use getResponseHeader('Location') and getAllResponseHeaders(), but unfortunately, both return null values. Below is my current implemen ...

Include a new variable in a JavaScript email template

Is there a way to include the variable bob in the body of an email? var bob; function sendMail() { var link = "mailto:YourEmailHere" + "?cc=" + "&subject=App Build Link Buit With MWFPRO's App Build Tool" + "&body=Hi ...

Perl's powerful capabilities allow for the creation of interactive drop-down menus

As a newcomer to web programming, I have recently been tasked with scripting projects during my summer internship. I've been utilizing Perl to develop CGI scripts for my company's internal website, specifically catering to the needs of the develo ...

What is the best way to handle multiple queries in NightmareJS?

The following JavaScript code utilizes NightmareJS to search a website for 3 posts and retrieve the username of the post uploader. var Nightmare = require('nightmare'); var nightmare = Nightmare({ show: true }); var inputArray = [198,199,201]; ...

Reactive form controls that are constantly changing and adapting to

Task Description: I need to create a form with two calendar inputs. When the user selects the range from May 2020 to May 21, I should generate 3 sets of 12 dynamic reactive form controls with required field validations. I am not experienced in working wi ...

Convert HTML element to a JSON object

Here is an example of an HTML element: <div class='myparent'> <div> <div class="pdp-product-price"> <span> 650 rupees</span> <div class="origin-block"> <sp ...

Only the first cell is affected by the background color setting

... <th style='background-color:#cccccc;font-weight:bold'><td></td><td>Address</td><td>Last Name</td><td>First Name</td><td>ZIP Code</td><td>City</td></th> ...

Bootstrap 4 - DropDown option - Element is positioned above the navigation bar

Currently facing an issue with the DropDown menu. Whenever I add padding to the DropDown menu (class - drop_link), it ends up pushing the entire element over the <nav>. I'm unsure of how to prevent this from occurring. I attempted to disable som ...

Altering calendar dates using VBA for web scraping

Seeking assistance with creating a VBA program for automatically downloading historical stock data from a website. The code I have currently works for downloading data, but I'm facing challenges in changing the date using my code. You can view the co ...

The Bootstrap modal-backdrop dilemma requiring JavaScript intervention

I am encountering some problems with the Bootstrap modal. When I try to open a modal, everything looks good, but when I close it, the screen turns completely black. Upon closer inspection, I found that every time I click on the X to close the modal, there ...

Guide to building an image carousel using HTML and CSS

Having trouble incorporating an image slider into my website. The images are 1650 by 350 in size and there are a total of 4 images with these dimensions. I attempted to implement this using HTML and CSS, but the images are not sliding automatically as inte ...

creating links using json2html

I need assistance to create a clickable anchor using json2html with the following transformation: 'renderTimeline':[ { tag: "a", class: "btn btn-warning btn-circle", style: "float: right;", html: "<i class=\"icon-rem ...

Achieving a more optimized display on mobile and desktop using Bootstrap 5 techniques

I am working with a bootstrap 5 website template and I need to arrange divs in the following order: Desktop https://i.sstatic.net/qQEiP.png Mobile https://i.sstatic.net/JUnCY.png <div class="d-flex justify-content-between align-items-center pt- ...

Cloud function -> time stamps evaluation

I've been working on a cloud function to delete items in the "links" collection that have an end time older than the current timestamp. Although my function runs without any errors, it's not deleting the items as expected and is causing me quite ...