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

The issue with the max-height transition not functioning properly arises when there are dynamic changes to the max-height

document.querySelectorAll('.sidebarCategory').forEach(el =>{ el.addEventListener('click', e =>{ let sub = el.nextElementSibling if(sub.style.maxHeight){ el.classList.remove('opened&apos ...

Exploring design techniques in React Native

I am currently part of a team working on a react native project with multiple contributors. My goal is to find an effective way to segregate the styling tasks from coding, allowing UI developers to work independently without relying on code developers. In ...

Using a Mixin as an Argument in Another Mixin in LESS CSS

Is it possible to pass the declaration of one mixin or style to another mixin as an input parameter? Consider the following example involving animation keyframes. This is how keyframes are typically defined in pure CSS: @-moz-keyframes some-name { fr ...

HTML5 will consistently display the video control panel

I am looking to keep the control panel of the html5 <video> element visible at all times while hiding the play/pause button. I attempted the following: <style> video::-webkit-media-controls-panel { display: flex !important; ...

I am looking to incorporate a feature in my form that allows for multiple file uploads and displaying them

Currently, I have a single file upload input field in my form and it works perfectly fine. However, I am looking to add multiple file inputs to the form as well. I have attempted various methods to integrate the code for multiple file uploads into my form, ...

Is there a way to incorporate cell highlighting on IE?

I've implemented a method to highlight selected cells based on the suggestion from jointjs. It surrounds the cell with a 2-pixel red border, which works well in Chrome. However, I need the outline to work in IE as well. Unfortunately, when I reviewed ...

Is it possible to establish a CSS minimum width that is relative to a different element?

Is it possible to set the min-width of a submenu in CSS to be equal to that of the corresponding link in a navigation menu? I am using LESS for my styling. <ul> <li> <a href="">Item FooBarBaz</a> <ul class="submenu"&g ...

Reading Properties in VueJS with Firebase

<template> <div id="app"> <h1 id="title"gt;{{ quiz.title }}</h1> <div id="ques" v-for="(question, index) in quiz.questions" :key="question.text"> <div v-show="index = ...

Setting the height of a div tag in CSS/HTML5 to match the current height of the browser window

Is there a way to make the height of my div tag always equal to the height of the browser's window, so it adjusts automatically when the browser is resized? I have tried using .class { height: 100%; } But this doesn't work. I've learne ...

Equal-height list items in a multi-column unordered list

A multi-column unordered list has been created. Below is the HTML code: <ul> <li>Antelope</li> <li>Bison</li> <li>Camel</li> <li>Deer</li> <li>Eland</li> <li>Gazell ...

Is the sequence of CSS styles important in styling web content?

I'm a newcomer to this field and recently encountered an issue with my div styling. My code was compatible with Chrome 11 but did not render properly in Firefox 4, Opera 11.1, and IE 8. Upon review, I found that the style for the div was as follows, a ...

What is the reason the .clearfix class fails to properly clear a floated element?

Check out this post: Understanding clearfix The most helpful response indicates that the clearfix should be applied directly to the last floating element: To incorporate a clearfix, you simply need to include HTML code: <div class="clearfix"> ...

What steps can be taken to stop 'type-hacking'?

Imagine owning a popular social media platform and wanting to integrate an iframe for user signups through third-party sites, similar to Facebook's 'like this' iframes. However, you are concerned about the security risks associated with ifra ...

Having trouble applying CSS while printing using the ngx-print library in Angular 14. Can anyone help me out with this issue

The table shown in the image is experiencing issues with applying CSS properties when printing. While the background graphics feature has been enabled, the preview section still does not reflect the CSS styling. What could be causing this discrepancy? Cli ...

Initial page load experiencing issues with paroller.js functionality

Upon hard refreshing the page in the middle section, there seems to be an issue with the paroller js transform: translateY(); CSS effect not functioning properly on the <div class="col col-6" data-paroller-factor="0.4" data-paroller-type="foreground" da ...

What is the best way to connect information from an HTML input field to a JavaScript object with the help of AngularJS?

As a beginner in AngularJS, I'm struggling to find the best approach to achieve my goal. I aim to create a grid of input tags with type=number in my HTML and have it set up so that whenever the value is increased, a new object is added to a list. Simi ...

section element is not receiving styles from the specified class

Whenever I want to ensure that a class is only used on specific elements, I usually prefix it with the element name. For example, using div.className limits the className styling to just div elements. However, when I tried the same approach with a section ...

The find function within $(this) is malfunctioning

I'm having issues with displaying/hiding content when clicking on a table row. Here is the simplified code snippet: HTML: <table> <tr onclick="showDetails()"> <td>Some text <br> <span class="hiddenC ...

Retrieve the Data from Input Fields with Matching Classes and Transmit to a Script Using AJAX Request

I am working on a form that includes multiple input fields: <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="da ...

Bootstrap dropdown menu fails to adapt to updated navbar height

I recently made a modification to the navbar size on my website, increasing it from 50px to 63px by tweaking a line in the bootstrap.css file. The exact code snippet I utilized for this adjustment is as follows: .navbar { position: relative; min ...