Which works fine when I add it to my WordPress footer.php file. However, when I enqueue the external JS file in my functions.php file, the icon no longer appears.
The issue is likely due to the timing of when you’re trying to load the animated icon and create the container for it. It seems that the script is loading before the element with id="animation-container" is present on the page.
To address this, you need to ensure that your Lottie script executes only after the container element is created on the page. You can achieve this by utilizing the wp_footer hook in your functions.php file:
In this example, the Lottie script is enqueued in the footer of the page. The animation-container element is then created when the page is being rendered, as part of the wp_footer hook.
Here’s the sequence of events:
The Lottie script is loaded in the footer of the page.
As the page is being rendered, the animation-container element is created.
Subsequently, the Lottie script is initialized and loaded within the created container.
Give this approach a try, and your animated icon should appear on your site when using the script enqueueing method through functions.php. Let me know if it helps!