aos icon indicating copy to clipboard operation
aos copied to clipboard

aos:in event does not fire if the animated element is on screen when the page loads

Open akrikorian85 opened this issue 5 years ago • 19 comments

This is:

  • Bug

Specifications

  • AOS version: 3.0.0-beta.5
  • OS: Windows 10 Pro
  • Browser: Chrome 69.0.3497.100 (Official Build) (64-bit)

Expected Behavior

I was expecting aos:init to fire

Actual Behavior

aos:in event does not fire if an animated element is on screen when the page loads. I am calling AOS.init() and adding the event handler on DOM ready. I am using the data-aos="fade-up" attribute

Steps to Reproduce the Problem

AOS.init(); document.addEventListener('aos:in', ({ detail }) => { console.dir(detail); });

On Chrome, scroll to the animated elements and refresh the page. They will fade up, but you will not see the element being logged in the console.

Detailed Description

Possible Solution

Is it racing? Are you triggering the event on scroll?

akrikorian85 avatar Sep 28 '18 18:09 akrikorian85

Thanks for posting this issue @akrikorian85 ! I'll check it out

michalsnik avatar Oct 04 '18 00:10 michalsnik

Got the same behaviour with this configuration:

  • AOS version: 2.3.3
  • OS: MacOS 10.12.6
  • Browser: Chrome 69.0.3497.100 (Official Build) (64-bit)

ibanjo avatar Oct 04 '18 10:10 ibanjo

Same, when page loads it stays hidden the elements that should be shown. They only appear on scroll. So it appears an empty page for the user at the beginning.

Zokor avatar Oct 14 '18 21:10 Zokor

Hello, any sugestions?

AFlorencia avatar Oct 18 '18 15:10 AFlorencia

You can this for example:

$("body,html").animate({ scrollTop: 1 }, 0);

for it to do a scroll without the user noticing. if can after that do a scroll top to zero after for him not to notice completly :)

With this "hack" it will make your elements be shown.

Zokor avatar Oct 18 '18 16:10 Zokor

I can confirm that the same issue is still present in the beta6, win 10, osx, safari, chrome

Larzans avatar Jan 18 '19 18:01 Larzans

In my case it was not a bug but due to the fact that i was waiting for the document.ready event to hook into the aos:in event, but obviously, when the document is ready it already has executed AOS.

So what you (at least I) have to do is put your event listeners OUTSIDE of the code that waits for document.ready and define it ASAP, in your case probably BEFOR the AOS.init() call.

You don't have to wait for AOS to be loaded / created because you can start listening to events before they are defined / exist, and while AOS is initializing it is apparently also executing the necessary animations of already visible elements!

See if that helps in your case to (would be good to provide an example of your problem to test it on that)

Larzans avatar Jan 18 '19 19:01 Larzans

is this going to be fixed?

mark-chief avatar Feb 11 '19 12:02 mark-chief

Merge this issue with mine and mark resolved.

If you have the AOS.init() function call in a plugin file or have it being declared before another JS library like slick or parallax you have to give AOS priority so make sure that function is declared in the outermost layer of the script tag in your html file. And the called last at the end of your js file as well to not be intertwined with any other obstacles causing glitches or bugs. If its simply in a plugin file cut it and paste it in its own file. Then last after the end of your </body> tag <script src="YourJSFile"></script> at the outermost layer.

HeavenlyEntity avatar Jul 29 '19 16:07 HeavenlyEntity

is this going to be fixed?

AOS needs priority so make sure you are calling a IIF to make sure that there are no issues.

HeavenlyEntity avatar Jul 29 '19 17:07 HeavenlyEntity

This might not be the best answer, but I'll tell you what worked for me (using Nuxt.js):

TLDR:

Change startEvent to load and once to false

My Code

import Vue from 'vue'
import AOS from "aos"
import "aos/dist/aos.css"

Vue.use(AOS.init({
  disable: false,
+ startEvent: 'load',
  initClassName: 'aos-init',
  animatedClassName: 'aos-animate',
  useClassNames: false,
  disableMutationObserver: false,
  debounceDelay: 50,
  throttleDelay: 99,

  // Settings that can be overridden on per-element basis, by `data-aos-*` attributes:
  offset: 120,
  delay: 0,
  duration: 400,
  easing: 'ease-in-out-quad'
+ once: false,
  mirror: false,
  anchorPlacement: 'top-bottom'
}))

This way, any time I hit refresh, press the back button and land on a page, navigate to a page or load a page for the first time all the AOS elements spawn in correctly, even if they're in view when the page loads.

I'm assuming that the main drawback to this approach is that if you have a lot of AOS elements they basically have to be loaded in before the page is updated, so it's probably not optimal. But again, it works.

Thoughts?

mtpiercey avatar Jan 15 '20 02:01 mtpiercey

no solution worked for me, i'm just creating a basic website the only library i have is AOS, nothing should interfere, someone could help me, please.

alexiscanasz avatar May 06 '20 06:05 alexiscanasz

In my case it was not a bug but due to the fact that i was waiting for the document.ready event to hook into the aos:in event, but obviously, when the document is ready it already has executed AOS.

So what you (at least I) have to do is put your event listeners OUTSIDE of the code that waits for document.ready and define it ASAP, in your case probably BEFOR the AOS.init() call.

You don't have to wait for AOS to be loaded / created because you can start listening to events before they are defined / exist, and while AOS is initializing it is apparently also executing the necessary animations of already visible elements!

See if that helps in your case to (would be good to provide an example of your problem to test it on that)

U saved my day

cupok avatar Dec 10 '20 03:12 cupok

working after remove css class preloader

    <script>
      var overlayEvent = new CustomEvent("overlayLoaded");
      $(function () {
        $(".preloader").fadeOut(1000, function () {
          document.dispatchEvent(overlayEvent);
          $("body").removeClass("ovh");
        });

        AOS.init({
          startEvent: "overlayLoaded", // name of the event dispatched on the document, that AOS should initialize on
        });
      });
    </script>

ahmedhbakr avatar Dec 23 '20 00:12 ahmedhbakr

This might not be the best answer, but I'll tell you what worked for me (using Nuxt.js):

TLDR:

Change startEvent to load and once to false

My Code

import Vue from 'vue'
import AOS from "aos"
import "aos/dist/aos.css"

Vue.use(AOS.init({
  disable: false,
+ startEvent: 'load',
  initClassName: 'aos-init',
  animatedClassName: 'aos-animate',
  useClassNames: false,
  disableMutationObserver: false,
  debounceDelay: 50,
  throttleDelay: 99,

  // Settings that can be overridden on per-element basis, by `data-aos-*` attributes:
  offset: 120,
  delay: 0,
  duration: 400,
  easing: 'ease-in-out-quad'
+ once: false,
  mirror: false,
  anchorPlacement: 'top-bottom'
}))

This way, any time I hit refresh, press the back button and land on a page, navigate to a page or load a page for the first time all the AOS elements spawn in correctly, even if they're in view when the page loads.

I'm assuming that the main drawback to this approach is that if you have a lot of AOS elements they basically have to be loaded in before the page is updated, so it's probably not optimal. But again, it works.

Thoughts?

Worked like a charm. Thanks

izzatullokanoatov avatar Jan 05 '21 07:01 izzatullokanoatov

Changing the startEvent to load fixed this for me. No need to update once.

ineptian avatar Feb 02 '21 16:02 ineptian

adding the following line in my js file solved the issue window.addEventListener('load', AOS.refresh());

ghoshprithwi avatar May 19 '21 10:05 ghoshprithwi

I solved this with setTimeout because must use once: true.

setTimeout(function() {
    AOS.init({
        once: true,
    });
}, 100);

adt-mgpark avatar Oct 27 '21 10:10 adt-mgpark

If you explicitly add aos-init aos-animate css classes to an element, then this element will not animate if it is in viewport when the page loads.

This can happen for example if you copy code from browser developer tools.

Those classes are added to animated elements by AOS automatically.

olegovk avatar Feb 12 '22 14:02 olegovk

If you explicitly add aos-init aos-animate css classes to an element, then this element will not animate if it is in viewport when the page loads.

This can happen for example if you copy code from browser developer tools.

Those classes are added to animated elements by AOS automatically.

Thank you so much! This comment saved me so much debugging. I was tasked with redoing a page from React into a Drupal Twig template and had to scrape the HTML created by React. I had no idea that the AOS library added these classes after the animation (AOS was implemented prior to me coming on board with the project, so not familiar with it).

JackG102 avatar Mar 09 '23 12:03 JackG102

In my case it was not a bug but due to the fact that i was waiting for the document.ready event to hook into the aos:in event, but obviously, when the document is ready it already has executed AOS.

So what you (at least I) have to do is put your event listeners OUTSIDE of the code that waits for document.ready and define it ASAP, in your case probably BEFOR the AOS.init() call.

You don't have to wait for AOS to be loaded / created because you can start listening to events before they are defined / exist, and while AOS is initializing it is apparently also executing the necessary animations of already visible elements!

See if that helps in your case to (would be good to provide an example of your problem to test it on that)

Thank you, worked like a charm, with newest version of AOS (v3 beta-6). It's better solution than changing startEvent to "load", because in that way it will take too long to start the animations.

hasenov avatar Aug 22 '23 15:08 hasenov

It's been a while. I'll close this issue as it looks like the fix is just to define event listeners before AOS.init(). Thank you all!

akrikorian85 avatar Aug 22 '23 16:08 akrikorian85