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.

Trying to add JS Function to ID

See original GitHub issue

Hey, I’m very new to Javascript and I’ve been trying to create an animated drop-down with bodymovin. I added id’s (#) to my layers in after effects and I want to add hyperlinks and anim.playSegments to some of those ids but I can’t seem to make it work. Any ideas? They are all in the same json.

EDIT: I managed to do it but it only works on Chrome, it doesn’t work on Firefox nor Opera and probably the others too, this is the code I used, please help out if you know what is the problem.

Code:

// Open Drop Down //
var jsonData = { "name": "hamburger" };
$(document).ready(function () {
    $("#hamburger").on("click", function () {
        anim.playSegments([[0,20]],true);
    });
});

// Close Drop-Down //
var jsonData = { "name": "closemenu" };
$(document).ready(function () {
    $("#closemenu").on("click", function () {
        anim.playSegments([[20,0]],true);
    });
});

EDIT 2: In Mozilla the code seems to work sometimes but when I refresh it doesnt work again, it’s confusing. It works fine in Opera and Chrome.

EDIT 3: It seems it’s because the files are too heavy but the gzipped version of bodymovin doesnt work for some reason.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
bodymovincommented, Sep 2, 2017

Hi @Madrise , as @Jeremy-Knudsen says, you need to attach your click events to the DOMLoaded event of the animation. When the jquery document ready event is fired, the animation is not loaded yet so your element with id “hamburguer” does not exist. After your code

anim = bodymovin.loadAnimation(animData);

You should do something in the lines of:

anim.addEventListener('DOMLoaded', function(){

$("#hamburger").on("click", function () {
  anim.playSegments([[0,20]],true);
  $("#bodymovin").css("z-index", "4");
  $("#bodymovin").css("transition", "z-index 0s step-end, opacity 0s linear");
  $("#hamburger").css("display", "none");
  $("#hamburger").css("cursor", "default");
  });


})
1reaction
Jeremy-Knudsencommented, Aug 31, 2017

Hi Madrise,

Can you share the link to your full code (either a link to your site or a pen on https://codepen.io)? This will help troubleshoot where things are going wrong. I can imagine a couple of problematic things that might be occurring.

First, the playSegments() method is attached to a loaded Bodymovin animation; not to individual layers within the animation. Therefore, I don’t believe you can call the playSegments() method from a layer within the loaded animation. Adding IDs to layers within the animation allow you to directly manipulate values of that layer via JavaScript, which is more advanced than what you’re trying to do.

Are you able to get the animations to load and playback all of the way through? What does your JavaScript code look like for loading your Bodymovin animations?

Second, you might be attempting to play an animation before it is loaded by Bodymovin. The document might report that it is “ready”, but Bodymovin animations still can be loading after the document ready state. This is why there’s a DOMLoaded event listener in Bodymovin (NOTE: This is not the same as DOMContentLoaded). DOMLoaded is an event listener that is specific to Bodymovin. Here is a function that I wrote that initializes mouse events only after the Bodymovin animation has loaded:

function initEvents (anim) { // DOMLoaded event listener that waits for the Bodymovin animation to load before I perform any actions on it anim.addEventListener('DOMLoaded', function () { // Define the element to add mouse and touch event listeners to, which is typically a parent element of the SVG; not the SVG itself. Note: "element" is the parent container of the SVG. I use "parentNode" to travel up the DOM. var parent = element.parentNode.parentNode; // Mouseover event listener parent.addEventListener("mouseover", function () { if (thisAnim.isPaused) { thisAnim.goToAndPlay(0); } // if isPaused conditional block }); // Mouseover event listener }); // DOMLoaded event listener };

Read more comments on GitHub >

github_iconTop Results From Across the Web

Onclick function based on element id - Stack Overflow
I'm working on a site with a visualization of inheritance relation. I try to link each nodebox with ...
Read more >
How to add an ID to element in JavaScript - Educative.io
Adding ID to a new HTML element ... First, create a new element. This can be done within HTML or with JavaScript. Use:...
Read more >
HTML DOM Element id Property - W3Schools
The id property sets or returns the value of an element's id attribute. Note. An id should be unique within a page. See...
Read more >
HTML Button onclick – JavaScript Click Event Tutorial
In JavaScript, you invoke a function by calling its name, then you put a parenthesis after the function identifier (the name).
Read more >
Pass Element ID from one function to another in the same ...
Or you could use js to find the previous element assuming that was the input. Or you could use the 'for' attribute of...
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