"Utilize JavaScript (Observer) to pick out an SVG element based on its class

I am working on dynamically displaying an SVG item as it enters the viewport using an observer. The animation associated with the SVG item should only play when it becomes visible in the view window. Initially, I have set the display property to none, and created a separate class to change the property to block. Adding this class manually through the inspector works fine. My goal is to automatically add the animation class front-social-start to the element's class list once it comes into view.

If anyone can help out, that would be greatly appreciated - currently fueling my work solely on caffeine due to this little bug XD

HTML

<div class="radial-graph-container">
            <svg width="200" height="200" class="chart-container">
              <circle cx="100" cy="100" r="90" class="back" fill="none" />

<!-- The below circle is the one I want to select -->

              <circle cx="100" cy="100" r="90" class="front-social" fill="none" />
              <g class="graph-center-txt">
                <text
                  x="100"
                  y="100"
                  alignment-baseline="central"
                  text-anchor="middle"
                  id="percentage-social-media-usage"
                >
                  0%
                </text>
              </g>
            </svg>
</div>

CSS

.back {
  stroke: #101114;
  stroke-width: 10;
}

.front-social {
  stroke: #e96f31;
  stroke-width: 15;
  stroke-dasharray: 395.84;
  transform: rotate(-90deg);
  transform-origin: center;
  animation: fill 1.7s reverse;
  display: none;
}

.front-social-start {
  display: block;
}

JS

const el = document.querySelector('.font-social');
          const observer = new IntersectionObserver(entries => {
            // Loop over the entries
          entries.forEach(entry => {
            // If the element is visible
          if (entry.isIntersecting) {
          //   Add the animation class
          el.classList.add('front-social-start');
            }
          });
        });

        observer.observe(document.getElementsByClassName('.front-social'));

Answer №1

Implemented a unique identifier for radial-graph-container:

id="radial-graph-containersm"

Utilizing an approach to hide the entire div and only show it when it comes into view, striking a balance.

Updated observer functionality to assign a class to the item based on its ID.

CSS

.radial-graph-container {
  display: flex;
  justify-content: center;
  flex-direction: column;
  padding-bottom: 2.4rem;
  display: none;
}

.radial-graph-start {
  display: block;
}

JS

const graphSm = document.querySelector('#radial-graph-containersm');
          const observer = new IntersectionObserver(entries => {
            // Iterate over the entries
          entries.forEach(entry => {
            // Check if the element is visible
          if (entry.isIntersecting) {
            // Apply the animation class
          graphSm.classList.add('radial-graph-start');
            }
          });
        });

Isn't it ironic how things unfold - struggled for hours before finding a solution just 20 minutes after seeking assistance. The wonders of coding...

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

Load JavaScript files in order within the <body> tag and trigger a callback function when all files have

Currently, my website (which is actually a Cordova/Phonegap app) has the following scripts in the <head>: <script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="appPolyfills.js"></script> ...

Exploring the integration of Glide js within a Next js environment

I am trying to figure out how to use a script tag in the next JavaScript _app.js component for utilizing the Glide JavaScript carousel. However, I am facing difficulties in using the script tag. Can someone help me out by providing guidance on how to inc ...

Position the navigation content slightly to the left and right using Bootstrap 5

Struggling to align links in a Navbar to different sides? I attempted using the classes "ms-auto" and "me-auto" on two lists, but had no success (everything stayed to the left, as shown in the picture). Essentially, I want the "Dropdown" item on the right ...

Steps to invoking an asynchronous function within another async function

Is there a way for me to transform the following function into an Async function? I need to invoke several methods based on the result of the function call when isMaxAttemptExceeded has been fully executed. let isMaxAttemptExceeded = async () =&g ...

I am attempting to integrate a datetimepicker onto my website, but I keep encountering an error. Can

Once upon a time, my website had a perfectly functioning datetimepicker. However, one day it suddenly stopped working. After some investigation, I realized that the JavaScript file I was referencing had been taken down. Determined to fix the issue, I visit ...

Preventing the use of scripting tags in text boxes

I'm currently trying to integrate the post feature on my website using codeigniter (PHP). However, I've run into an issue where when I input and submit the following code: <script> alert("aaaa"); </script> The JavaScript starts exec ...

collaborate and coordinate a territory among various components on a map

I'm currently working with an array of elements that are being drawn on a canvas. export function useCanvas(){ const canvasRef = useRef(null); const [ elements, setElements] = useState([]); const [ isHover, setIsHover] = useState(false); ...

Creating a responsive HTML, CSS, and JavaScript popup: A step-by-step guide

I'm facing a challenge in making my HTML, CSS, and JavaScript popup responsive. Despite looking into various resources, I haven't been able to find a solution that fits my code. However, I am confident that it is achievable to make it responsive! ...

Utilizing Ajax to Populate a Dropdown List with Data

Currently, I am retrieving data from an API. The data consists of IDs and charity names that need to be displayed in a dropdown list. While I have successfully shown the charity names in the dropdown list, I am struggling with including the IDs. How can I ...

Choosing a recently inserted row in jqGrid

After reloading the grid, I am trying to select the newly added row, which is always added at the end. However, it seems impossible to do so after the reload. Is there a reliable way to select the last row after reloading the grid? The current code I have ...

Having Numerous HTML Tables All in One Page Accompanied by Written Content

I used to have a webpage with multiple (4) HTML tables all contained within one div that spanned the entire page, serving only as an indicator for the tables' starting point. It all worked perfectly until a recent Chrome update caused chaos with the t ...

Create a scrollbar that allows for both vertical and horizontal movement within the canvas

I am looking to create a canvas with scroll bars. The canvas is sized at 600 x 800, and the parent div is sized at 200 x 200. My goal is to have both horizontal and vertical scroll bars on the canvas. I've tried using the script provided, but unfor ...

The application rejected the styling from 'http://localhost:3000/css/style.css' due to its unsupported MIME type ('text/html') for a stylesheet

While working on an example website template, I encountered an error in the Chrome console: Refused to apply style from 'http://localhost:3000/css/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, ...

Sending numerous POST attributes utilizing identical parameter name

Currently, I am facing a situation that requires me to send POST parameters with the same name. Can anyone provide guidance on how this can be accomplished? I have successfully handled a similar scenario with a GET request by constructing the URL and appen ...

What is the best way to redirect a URL parameter that contains "?m=1"?

After transitioning from blogger to wordpress, I've noticed that many incoming links now have a "?m=1" parameter added to the end of my post link. For example: http://www.example.com/2015/06/name-of-blog-post.html?m=1 I've tried searching for ...

Failure to highlight items when using the multiple select function

After adding a select all button to a multiple select, I encountered an issue. Although all the items are being selected, they are not highlighted when clicking on the select all button. Below is the code snippet: <div class="label_hd">Profiles* {{ ...

Is it possible to customize the Menu hover effect in Element Plus and Vue?

Hello everyone, I'm a beginner with Vue, HTML, CSS, and Element Plus. I am trying to customize a Side Bar Menu with my own hover effect, but it doesn't seem to be working. Whenever I change the background color of the el-menu element, the hover e ...

Extract the year from a string formatted as 1880-01-01T00:00:00.000

Looking to extract the year from an array of dates with format 1880-01-01T00:00:00.000. What's the most efficient method to accomplish this using JavaScript? ...

Mobile view causes malfunction in Bootstrap Toggle Burger Menu functionality

<nav class="navbar navbar-expand-lg navbar-dark px-2 " style="background-color:#E53935;"> <a class="navbar-brand" href="#">FoodFusion</a> <button class=&quo ...

Using HTML5 to create a structured outline with numbered headings at multiple levels

Currently, I am in the process of creating an HTML5 manual that will require numbering for various elements. The main headings should be numbered like "Section 4: Some Stuff" Subheadings will also need numbers such as "4.01: the first point you need to kn ...