Implementing a background animation on click in React

I'm trying to create a link that, when clicked, will trigger a CSS animation to start or change its state from pause to play. Here is my current approach:

import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import './main_page.css'

function Main() {

    var bg = document.getElementsByClassName('app');

    return (
        <div>
            <h1>
                Welcome to the main page!
            </h1>

            <h2>
                Please note that this website is still in development. Stay tuned for updates!
            </h2>

            <p>
                In the meantime, feel free to explore my project here! <br></br>
                <Link to={'/projects/scatter_gather'} onClick={() => {
                    bg.animationPlayState = 'running';
                }}>
                    Visit "Scatter-Gather"
                </Link>
            </p>
        </div>
    );
}

export default Main;

The CSS code for the .App class looks like this:

.App {
  background-image: linear-gradient(45deg, rgba(255,179,105,0.5) 0%, rgba(245,255,122,0.5) 20%, rgba(121,252,255,0.5) 40%, rgba(83,92,255,0.5) 60%, rgba(255,118,234,0.5) 80%, rgba(255,131,131,0.5) 100%);
  background-size: 400%;
  animation: background_animation 12s infinite;
  animation-play-state: paused;
  animation-direction: alternate;
  color: rgb(68, 68, 68);
}

@keyframes background_animation {
  0% {
    background-position: 0%;
  }
  20% {
    background-position:  20%;
  }
  40% {
    background-position: 40%;
  }
  60% {
    background-position: 60%;
  }
  80% {
    background-position: 80%;
  }
  100% {
    background-position: 100%;
  }
}

However, I am facing an issue where clicking on the link does not seem to change the property as intended. Could it be a syntax error? Any help would be greatly appreciated!

Answer №1

Would you be willing to give this a shot?

<p>
                In the meantime, take a look at my latest project! <br></br>
                <Link to = {'/projects/scatter_gather'}>
                    <span onClick={() => {
                    bg.animationPlayState = 'running';
                }}>Check out "Scatter-Gather"</span>
                </Link>
            </p>

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

How can you apply filtering to a table using jQuery or AngularJS?

I am looking to implement a filtering system for my table. The table structure is as follows: name | date | agencyID test 2016-03-17 91282774 test 2016-03-18 27496321 My goal is to have a dropdown menu containing all the &apo ...

Array scripts are removed once data has been extracted from the CSV file

Having trouble reading a CSV file using file reader in Javascript. I wrote a script that declares arrays and updates them as the file is read. However, once the reading is complete, all data from the arrays gets wiped out. I'm quite new to JS and can& ...

What is the best way to eliminate a JSON header?

Here is the JSON data I have: { "folder": [ { "$": { "id": "471841542", "name": "Manajemen Pemasaran", "description": "", "user_id": "186868958", "shared": "1", "shared_l ...

"What is the syntax for pushing an object onto an array within an array in AngularJS

Looking to create an object structure like the one below: $scope.allBooks = { books:[ { name:"", id:"" } { ...

PlaneGeometry at x=0 y=0 z=0 for three.js on a flat surface

Hi there! I have a code snippet that currently renders an image vertically, and I'm looking to modify it so that the PlaneGeometry is drawn horizontally instead. Rather than rotating it with mesh.rotation.x=THREE.Math.degToRad(-90);, I'd like it ...

Problems with proxy functionality for routing requests between ports

Currently working on a project that involves developing a web app using the MERN stack as part of a course I am taking. My backend is set to run on port 5000 while React is running on port 3000. The issue arises when trying to make a request to the API fr ...

Guide on adding a parent class to style all generated CSS rules in styled-components 5.3.5

When my application loads into a parent div with a specific class (for example, .my-app), is there a way to add the prefix .my-app to all classes generated by styled-components? For instance, consider a component like this: import React from 'react& ...

Align the image in the middle using JavaScript for Firebase

I am struggling to display the center of an image within a circle-shaped <img> tag with a border-radius of 50%. The issue arises when trying to display an image fetched from Firebase storage, rather than from a URL. In attempt to solve this problem, ...

Is there a way to extract the text from the inner div of an element using nightwatch.js?

I'm attempting to retrieve the content of a cell within a table, with the following CSS structure: <div data-testid="cellvalue_row-1_col-0" class="Table-cellContent" xpath="1"><span data-testid="tableCellCon ...

Exploring the process of altering the root path page in NextJS

Currently, I am working on a small project using Next.js. When the root path is requested in Next.js, it automatically looks for a file named index.js in the pages folder to display. However, I would like to show the user a different page named home.js in ...

Updating results in Angular.js based on variable changes

After setting some data in the window, I am facing issues with updating an angular.js table created using the smart table module/plugin. The data is structured like this: window.checker={};window.checker.checked = [{'ip':9,'port':0}] ...

Arranging the table based on the values of the key

I have already discovered that using the sort function is necessary to sort the table onClick. My goal is to sort by the key so that I can avoid repeating the code to sort the function by key. I am trying to keep my code DRY. How can I achieve this efficie ...

The custom error page in NextJS is failing to display

In my custom pages/404.ts file, I have coded the following: export default function NotFound() { return <h1>404 - Page Not Found</h1> } Additionally, there is another page that displays a 404 error when the organization is null: import Error ...

Tips for creating an output directory when the TypeScript files are stored in the './src' location

Here is what I currently have: ./src/myfile.ts ./test/mytest.spec.ts I want tsc to output a javascript file (myfile.js) and a definition file (myfile.d.ts) in the ./build directory. This is my tsconfig.ts: { "compilerOptions": { "module": "common ...

Transferring JavaScript files via Node server using NowJS

As someone new to Node, I need some help with a server-client web application I'm creating for a board game using Raphael for graphics. The issue I'm facing is that while the server successfully sends the HTML file in response to requests, the b ...

What is the best method to modify and access imported .css files within the WordPress style.css file?

I could use some assistance with a project involving minor edits to a WordPress website. I've hit a roadblock and need some help. The style.css file in WordPress doesn't have much CSS code, but instead imports files like this: /*Animate*/ @impo ...

Issue with RN-fetch-blob regarding base64 encoding specifically on Android devices

I've encountered an issue in React Native where I'm trying to download a base 64 string from an API and display it as a PDF on mobile devices. The problem arises when using the code on Android, as it returns a 'bad base 64' / invalid P ...

Convert a string that represents a Buffer into an actual Buffer object

I am looking for a way to change a string representing a Buffer into the actual encoded string. For instance, if I start with the string var str1 = "hello,there" And then convert it into a buffer using Buffer.from() buf1 = Buffer.from(str1) <Buffer ...

Automating the click of JavaScript buttons with Selenium

Test page Experimenting with the above Indeed link to test my selenium automation skills. I am attempting to automate the clicking of the 'apply' button using the Firefox webdriver. My code snippet is as follows: from selenium import webdriver ...

Ways to incorporate universal accessed screens using bottom tab navigator

I am in the process of developing an application that utilizes bottom tabs for main navigation. Within this application, there are certain screens that need to be accessible globally from various tabs or locations. One key feature is the ability to navigat ...