Unable to activate a CSS animation using JavaScript

Attempting to create a left-moving div triggered by JavaScript, everything appears correct but it's not functioning properly. Here's the code:

<html>
<head>  
<style>
#container {
  width: 100%;
  height: 500px;
  position: relative;
  overflow:hidden;
  background-color:black;
  border-radius:10px;
}
#item {
    width: 100px;
    height: 100px;
    position: absolute;
    border-radius:10px;
    overflow:hidden;
}
#stars {
    width: 200%;
    height: 500px;
    position: absolute;
    background-image: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);
    display:block;
    background-size:contain;
    bottom:0;
    left:0;
    right:0;
    z-index:1;
    background-repeat:repeat-x;
}
h1 {
  font-family: montserrat;
}
@keyframes stars{
    100%{transform:translateX(-1200px);}
}
</style>
</head>
<body bgcolor=white>
<h1>JavaScript Animation</h1>
    <p>
        <button onclick="myMove()">Animate</button> 
        <button onclick="stop()">Stop</button> 
    </p>
<div id = "container">
    <div id="stars"></div>
</div>
<script>
    function myMove() {
var stars = document.getElementById("stars");
stars.style.animation ="stars 10s linear infinite";
}
</script>
</body>
</html>

Using CSS alone will animate it continuously; desired to start animation with a button click.

Answer №1

let starObject = document.getElementById("stars");
let stars = document.getElementById("stars");

let stars = document.getElementById("stars");

function animateStars() {
  stars.style.animation = "stars 2s linear infinite";
}

function stopAnimation() {
  stars.style.animation = "none";
}
#container {
  width: 100%;
  height: 500px;
  position: relative;
  overflow: hidden;
  background-color: black;
  border-radius: 10px;
}
#item {
    width: 100px;
    height: 500px;
    position: absolute;
    border-radius: 10px;
    overflow: hidden;
}
#stars {
    width: 200%;
    height: 500px;
    position: absolute;
    background-image: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);
    display: block;
    background-size: contain;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1;
    background-repeat: repeat-x;
}
h1 {
  font-family: montserrat;
}
@keyframes stars{
    100%{transform: translateX(-1200px);}
}
<body bgcolor=white>
  <h1>Javascript Animation</h1>
  <p>
    <button onclick="animateStars()">Animate</button> 
    <button onclick="stopAnimation()">Stop</button> 
  </p>
  <div id ="container">
    <div id="stars"></div>
  </div>
</body>

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 of Angular finalize not functioning when used within a pipe alongside switchMap

Once the user confirms, I want the process to begin and keep the modal busy until it's complete. However, the current code does not function in this manner. The isModalBusy condition only turns false when an HTTP error is returned from the service. In ...

Certain URLs do not receive requests when using jQuery.ajax(), while others are successful

Our Rails application features inline editing, allowing users to submit changes back to the server via PUT requests. The URL for the PUT request varies based on the object being edited and the page the user is on. The same JavaScript code supports this fea ...

When using jQuery to enable contenthover on divs, they will now start a new line instead of

I've been working on achieving a layout similar to this, with the contenthover script in action: Mockup Draft Of Desired Look However, the result I'm getting is different from what I expected, it can be seen here. The images are not aligning co ...

Is it Possible to Utilize Gridster Across Multiple Tabs?

My goal is to build several dashboards using angular-gridster2 in angular 8. Each dashboard will be located within a separate tab, and I am currently encountering some issues with this setup. For example, if the first tab has 3 gridster items and the sec ...

create a division in the organization of the identification numbers

Is there a way to always change pages when the id changes in a foreach loop to separate the printed pages? Take a look at this code snippet: var data = [ {Id: "552", valor: "50.00", Descricao: "Fraldas", }, {Id: "552", valor: "35.00", Descrica ...

pm2 initiates two identical processes using distinct versions

I am currently using pm2 version 4.2.3 Upon executing the command: pm2 start node launchers/engine_launcher.js --name "engine", it initiates the following processes: id │ name │ namespace │ version │ mode │ pid - ...

Looking to swap out innerHTML in a div multiple times with javascript?

When dealing with a div element that appears multiple times in your HTML: <div class="myclass">abc</div> <div class="myclass">abc def</div> ... How can I replace "abc" for all occurrences, rather than just the first one? (I' ...

How to implement Bootstrap 5 Floating Label in Contact Form 7 for maximum impact?

I'm having trouble with the new form feature, specifically Floating labels (Bootstrap 5) in Contact Form 7 on Wordpress. The issue arises when a <p> tag is added inside the <input> tag. It seems to break the functionality, but I'm not ...

Discovering the selected option's value in a dropdown and utilizing it to search through an array for data to fill another input

Is there a way to utilize the value of a select input option, created by a forEach loop, to extract the value of another property in the same array object and assign it as the value of a different input field? Apologies if this seems verbose or has been a ...

How can one selectively apply a class to the initial DIV within a collection of dynamically generated DIV elements without relying on jQuery?

I am currently working on implementing a slideshow within a modal using AngularJS and Bootstrap. My challenge lies in dynamically creating DIVs, but specifically adding the active class to only the first DIV. Is there a way to achieve this without relying ...

Interact with the exe/jar files on a remote machine using JavaScript

I've been tasked with finding a way to access an executable file (or any file located in a specific location like the C drive) directly from a web browser. The idea is to have a button on a webpage that, when clicked, will run the executable and pass ...

The multiple lines text box displays an input box

Having a simple issue here, I've set the value of an input text box, but when the page loads it is displaying in multiple lines, and this seems to be happening only on Chrome browser. Can anyone offer any assistance? This is the HTML code for the ...

Navigating through layers of nested data in Vue.js and assigning values to a multi-layer JSON object can be tricky

In my Vue component childEle, there is a complex form structure stored in the data: data:{ form:{ root1:Number, root2:{ child1:[], child2:{}, child3:String }, root3:[], root4:{ child4:string, child5:[] ...

Issue with Angular router failing to load the correct component

As a novice with Angular, I have the following routes set up. app.routing.module.ts import { NgModule } from '@angular/core'; import { RouterModule } from '@angular/router'; import { FrameComponent } from './ui/frame/frame.compon ...

Ways to incorporate validation into Material-UI form input fields

I have a react functional component that utilizes Material UI form, which includes TextField. The onChange event is managed in the "Container Component". I have added required for client-side form validation, but it doesn't seem to be working. Do I ne ...

Managing Cross-Origin Resource Sharing in web socket communication

While working on a client and server application that communicate over web sockets, I discovered that addressing CORS only on the web socket did not result in any additional CORS issues compared to handling CORS on the server side itself. Prior to resolvi ...

Unable to display socket data in Angular js Table using ng-repeat

div(ng-controller="dashController") nav.navbar.navbar-expand-sm.navbar-dark.fixed-top .container img(src='../images/Dashboard.png', alt='logo', width='180px') ul.navbar-nav li.nav-item ...

Update the default GET URL structure in Backbone

REST API: If you need to access my REST API, you can do so by sending a GET request to the following URL: http://localhost/Project/index.php/rest/resource/car This will retrieve all the data in the table in JSON format. Additionally, you can get specific ...

Is there a way to manage the iteration of $scope.$watch?

I'm currently facing an issue with controlling the $scope.$watch function in my code. It seems to be causing an infinite loop during iteration. $scope.$watch('dashboards', function(newVal, oldVal) { if (newVal !== oldVal) { ...

When three.js edges and text appear jagged or blurry while rotating

As a newcomer to three.js, I'm facing an issue with applying an html canvas as a texture to plane geometry. Everything works smoothly when there's no rotation involved. However, upon rotation, two problems arise: a) the outer edges of the mesh ap ...