Initiating CSS animation from the start

Having an issue with the "Start button" that plays both animation and background sounds. When I pause it, the animation does not stop where it left off and starts from the beginning again.

I tried using animation-play-state: pause and animation-fill-mode: forwards but I must be doing something wrong because it's not working as expected.

function startAnimation(){
   var audio = document.getElementById("audio");
   audio.play();
   document.getElementById('target').className ='animated';
}

function pauseAnimation(){
   var audio = document.getElementById("audio");
   audio.pause();
   document.getElementById('target').className ='noo';
}
button {
    background-color: darkgreen; 
    border-radius: 50px;
    border: none;
    color: white;
    padding: 13px 50px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    box-shadow: 0 3px 3px 0 rgba(0,0,0,0.4), 0 2px 1px rgba(0,0,0,0.1);
    }

button:hover{
    background-color: green; 
    border-radius: 50px;
    border: none;
    color: white;
    padding: 13px 50px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    cursor: pointer; 
    box-shadow: 0 6px 6px 0 rgba(0,0,0,0.25),
        0 0 4px 2px rgba(0,0,0,0.05);
    transform: translateY(-2px);
  }
p {
  position: relative;
  display: inline-block;
  font-size: 18px;
  font-weight: 80;
  color: black;
  overflow: hidden;

background: linear-gradient(to right, darkgreen, darkgreen 50%, black 50%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-size: 200% 100%;
background-position: 100%;

}
.animated {
    transition: background-position 9000ms linear;
    background-position: 0 100%;
    animation-fill-mode: forwards;
}
.noo > {
    -webkit-animation-play-state: paused;
    animation-fill-mode: forwards;
}
<button type = "button" id= "ST" onclick= "startAnimation()" >Start the story!</button><br><br><br><br>

<p href="#a" id="target">Got this simple problem, but I lack experience in CSS to solve it. Attaching the code below for reference </p> 

<audio id="audio" src="https://k15xf2.webwave.dev/lib/k15xf2/Prob1-kgs9f12v.mp3"></audio>

<button type = "button" id= "ST1" onclick= "pauseAnimation()" >Pause the story!</button><br><br><br><br>

Answer №1

It seems like there might be some confusion between transitions and animations. The animation-play-state property is specifically for controlling animations, not transitions.

When you add the class animated to set the background-position, a transition will occur. If you pause by removing the animated class and adding only the noo class, which doesn't have a background-position attribute, the element will revert back to its original position as per the default style (p).

To resolve this issue, I recommend using a proper animation approach by defining a @keyframes rule that transitions from the initial background-position to the desired one (e.g., 0 100%).

Furthermore, instead of completely removing the animated class from the element, consider keeping it along with the addition of the noo class to halt the animation effect.

Below is an example code snippet showcasing these recommendations:

function startAnimation() {
  var audio = document.getElementById("audio");
  audio.play();
  document.getElementById('target').className = 'animated';
}

function pauseAnimation() {
  var audio = document.getElementById("audio");
  audio.pause();
  document.getElementById('target').className = 'animated noo';
}
button {
  /* Button styling properties */
}

button:hover {
  /* Hover state styling properties */
}

p {
  /* Paragraph styling properties */
}

.animated {
  animation-duration: 9s;
  animation-name: fill;
  animation-fill-mode: forwards;
  animation-play-state: running;
}

.noo {
  animation-play-state: paused !important;
}

@keyframes fill{
  from {
    background-position: 100%;
  }

  to {
    background-position: 0 100%;
  }
}
<button type="button" id="startButton" onclick="startAnimation()">Start Animation!</button><br><br><br><br>

<p id="target">Content with animated effects goes here.</p>

<audio id="audio" src="path/to/audio/file.mp3"></audio>

<button type="button" id="pauseButton" onclick="pauseAnimation()">Pause Animation!</button><br><br><br><br>

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

Issue with maintaining fixed space above navbar in HTML/CSS code

I'm struggling to make my navbar sticky on my website without any space above it. Can someone help me with this issue? Here is the URL to my site: My CSS looks like this: body { font-size: 12px; line-height: 22px; font-family: Arial, H ...

Design a navigation bar that seamlessly incorporates an image

I am struggling to achieve a seamless header design that flows from the logo. I have experimented with aligning images within containers and using long background images, but neither method has yielded satisfactory results. Is there a reliable way to cre ...

What is the process for adding information to datatables using jQuery?

I have been struggling to make a data table in jQuery function correctly. I am able to append data to the table, but the sorting, pagination, and search features are not working. Even though the data is visible in the table, it shows as 0 records. I am wor ...

Issue with Firefox: Click event not fired when resize:vertical is set while focusing

Issue: Firefox is not registering the first click event when a textarea has the following CSS: textarea:focus { resize: vertical; } Check out the demo: http://jsbin.com/wuxomaneba/edit?html,css,output The fix for this problem is straightforward - ju ...

django-avatar // insufficient amount of values for decomposition (anticipated 2, received 1)

Visit the Django Avatar documentation here Encountering an error in Django stating "not enough values to unpack (Expected 2, got 1) while attempting to open a specific html template: {% extends 'base.html' %} {% load account %} {% load avatar_ta ...

How to align an element to the bottom using CSS without relying on the parent's height

Is there a way to align an element to the bottom of its container without knowing the exact height? I'm trying to position the "links" div next to the logo text at its bottom, but since the logo size can vary and may even be an image, setting a fixed ...

Customize the background color of FullCalendar in a React application

I'm currently using FullCalendar in my react app to showcase a calendar with events displayed. Right now, I have a day grid set up and I'm interested in changing the background color of the grid. The background color for today's date is alw ...

Styling Material UI components with CSS is a great way to

I'm facing difficulties while working with Material UI components. Currently, I have a Material UI table and I want to center-align the text within it. The standard CSS of Material UI contains an element named MuiTableCell-root-60 which has a text-a ...

Use ajax to load a webpage into a specific div, then reload the same div after submitting

I have a webpage that includes hyperlinks and a content area with an id of "contentarea" where the results from these hyperlinks are displayed. When a user clicks on the "Search" link, the page search.jsp loads, which contains a submit button and a form ...

The getText method in Selenium seems to be malfunctioning as it is unable to retrieve any text content from

Struggling to capture the text of WebElement using two different methods, but both are returning blank strings. // Method 1 d.findElement(By.id("deliveryDate")).getTex(); // Method 2 WebElement deliveryDate = d.findElement(By.id("deliveryDate")); Sys ...

Incorporating the parent object into a nested JavaScript function

I have come across similar questions, but my situation seems unique to me. I have simplified my code and turned one line into a comment, but the core concept remains unchanged... function myFunction(options) { var button; options.widgets.forEach(f ...

"Using jQuery to trigger a click event on a specific column within

Welcome to my HTML table. <table id="status_table" class="table table-bordered"> <tr> <th>Serial Number</th> <th>Product Number</th> <th>Description< ...

`Set the body height to equal that of the entire document`

Currently facing an issue on the test page when scrolling down: The body element is set to have a height of 100%, but it only takes up the viewport's height, not the entire document height. The goal is for the page to always display at 100% of the d ...

the contents exceed the boundaries

As I work on creating a page to display a list of books, I am facing an issue where the content sometimes overflows out of the book-info box. This usually happens when a book's title is excessively long and goes beyond the set height of 140px. I am se ...

Utilizing HTML and CSS allows for creating a responsive layout with three elements such as this

Need help with CSS to align three elements in a row, where the first two are on the left and the third is on the right. The layout should adjust so that when the window size is smaller, the second element moves below the first while the third element stays ...

Exploring the power of Vue.js by utilizing nested components within single file components

I have been attempting to implement nested single file components, but it's not working as expected. Below is a snippet of my main.js file : import Vue from 'vue' import BootstrapVue from "bootstrap-vue" import App from './App.vue&apos ...

Issues with the background-image scroll feature not functioning as intended

Can anyone help me out with a CSS issue I'm having? I've set an image as my background on a webpage and I want it to scroll with the page. Even though I followed some online tutorials, it's still not working for me. It's been a while si ...

Maximizing space efficiency in Bootstrap 5

Hello there I have a query; I am currently utilizing Bootstrap 5 for my website, and it's working well. However, I would like Bootstrap to fill the entire page instead of just 80%; (Refer to image) https://i.sstatic.net/Iswmu.jpg <head> ...

Tips for adjusting the color of the sidebar in an HTML email template

My attempt at modifying the background color of my HTML email template has been focused on changing the sidebar's white color, rather than altering the background of the email body itself. This is the CSS code I have: @import url('https://fonts. ...

Utilizing Selenium for automating button clicks

When attempting to click a button represented by the following HTML code: <a href="javascript:submitPage('V','All Notes','');" class="viewFilter">All Notes</a> I have made multiple attempts to ...