question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I target a class or multiple SVG's on 1 page?

See original GitHub issue

Any examples of this? I read that you can target an ID or object but don’t quite understand how to do that. When I try it with ID it only does the first one and then stops. This is my attempt at trying to get it to do class.

       jQuery(function () {
         var myCallback;
         var myElement = document.getElementsByClassName("icon-vivus");
         new Vivus(myElement, {duration: 50, type: 'async' }, myCallback);
      });

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
maxwellitocommented, Dec 18, 2015

Back on my computer, I can write a snippet 😃

jQuery(function () {
  // Define your callback
  var myCallback = function () {};

  // Get your HTMLCollection of SVG to animate
  var myElements = document.getElementsByClassName("icon-vivus");

  // Go across them to create a Vivus instance
  // with each of them
  for (var i = myElement.length - 1; i >= 0; i--) {
    new Vivus(myElements[i], {duration: 50, type: 'async' }, myCallback);
  }
});
1reaction
maxwellitocommented, Dec 26, 2015

Sorry for the delay, xmas and stuff… Well played for finding a solution yourself. I’m just scared about optimization if a second click is triggered. Because it will recreate objects and reload SVG… it’s consuming a lot. What I recommend you is to create objects first, then play it when needed.

jQuery(document).ready(function() {
  var i,
    myVivusItems = [],
    myElementsVivusClick = document.getElementsByClassName("icon-vivus-click");

  // Go across them to create a Vivus instance
  // with each of them
  for (i = myElementsVivusClick.length - 1; i >= 0; i--) {
    myVivusItems.push(new Vivus(myElementsVivusClick[i], {duration: 50, type: 'async', start: 'manual'}));
  }

  // vivus svg animations on click
  document.getElementById("icon-collapse").addEventListener('click', function () {
    for (i = myVivusItems.length - 1; i >= 0; i--) {
      myVivusItems[i].reset().play();
    }
  });
});

I hope it’s clear, I’m bad to explain things sometimes. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple SVGs on a page - Stack Overflow
var img = document.getElementById ; "svg1"); // get the SVG document inside the img tag var ; contentDocument; // get that circle item...
Read more >
Best Practices for Working with SVGs - Bitovi
In your favorite vector program, start by naming the main layer group for your SVG (e.g. "avatar-female-left"). Naming this layer will create a ......
Read more >
class - SVG: Scalable Vector Graphics - MDN Web Docs
Assigns a class name or set of class names to an element. You may assign the same class name or names to any...
Read more >
The Many Ways to Link Up Shapes and Images with HTML ...
The only trick here is to make sure the <a> tag is inside the SVG markup and that the tag wraps the shape...
Read more >
Styling And Animating SVGs With CSS - Smashing Magazine
Working with inline SVG and CSS is a lot easier because the SVG can be styled and animated by targeting it with style...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found