Jump to content
Search Community

staggerFromTo does not work on element list

wmosquini test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

People,
I am making code to control a list of elements, this is part of my code:

 

	for (let target of vetorElementos) {
        vetorAnimacoes[contador] = new TimelineMax({paused:true}).staggerFromTo(target, .3, {opacity: 0, y: 150},{opacity: 1, y: 0}, .3);  
        // vetorAnimacoes[contador] = new TimelineMax({paused:true}).fromTo(target, 1, {scale:0, visibility: 'hidden', opacity: 0},{scale: 1, visibility: 'visible', opacity: 1});  
		observer.observe(target);
		contador++;
	}

 

In the commented part of the code (line 3), this part works without any problem, but the code part (line 2) it does not execute at all.
In staggerFromTo, if you replace the target directly with the querySelectorAll element, it will work, but not the result I need.

This only happens in the stagger, either from, to or fromTo.

Did I miss any details?

Link to comment
Share on other sites

1 minute ago, OSUblake said:

E isso não funcionará porque o elemento não tem tamanho.


        

 

 

This piece of code works perfectly, but executes all elements together.
This is exactly why I am using Stagger to perform the animation individually.

This is the problem line of code:

 

vetorAnimacoes[contador] = new TimelineMax({paused:true}).staggerFromTo(target, .3, {opacity: 0, y: 150},{opacity: 1, y: 0}, .3);  

 

 

4 minutes ago, OSUblake said:

Por que você está usando um staggger se existe apenas um elemento?

 

There are actually 6 elements.
.uk-grid> div (all 6).
Each has to perform the animation individually.

Link to comment
Share on other sites

5 minutes ago, wmosquini said:

This piece of code works perfectly, but executes all elements together.
This is exactly why I am using Stagger to perform the animation individually.

This is the problem line of code:

 

Again, there is nothing to stagger. There is only ONE element.

 

for (let target of vetorElementos) {
  console.log(target)
}

 

What is the animation supposed to do?

Link to comment
Share on other sites

47 minutes ago, OSUblake said:

If you're trying to do a stagger animation by rows, then you would need do something like this. It could be written in a more dynamic way, but I'm just showing the basics.

 

 

 

 

 

Exactly, I just need to do the animation, as I can only perform it when displaying the element in the viewport, was using the code I posted above.

I will try to make some changes similar to what you made, apparently it already helps me in this detail.

Thanks a lot for the help.

Link to comment
Share on other sites

53 minutes ago, OSUblake said:

If you're trying to do a stagger animation by rows, then you would need do something like this. It could be written in a more dynamic way, but I'm just showing the basics.

 

 

 

just a question.

Is there any way to map elements other than by class? Ex: .uk-grid> div?

The question is why this grid structure will be assembled automatically in php, depending on the amount of elements, so I won't be able to know how many elements will be inside the .uk-grid, so it gets a bit more complicated to use the querySelectorAll set in classes. , ideally if he mapped the tags.
Any suggestion?

 

Link to comment
Share on other sites

2 minutes ago, wmosquini said:

Is there any way to map elements other than by class? Ex: .uk-grid> div?

 

I guess you could get the position of every element using .getBoundingClientRect() and sort the elements into an array of rows. You can determine if an element is on a new row by comparing an element's .top value against the previous element's .top value.

 

So you would end up with an array of arrays.

const grid = [
  [element, element, element],
  [element, element, element],
  [element, element]
];

// inside a loop
const row = grid[i];
const element = row[0]; // first element in row
element.myAnimation = new TimelineMax({ paused: true })
  .staggerFromTo(row, 1, {}, {});

 

Does that make any sense? I might be able to make a demo of how to do that later if you need more help.

Link to comment
Share on other sites

45 minutes ago, OSUblake said:

 

I guess you could get the position of every element using .getBoundingClientRect() and sort the elements into an array of rows. You can determine if an element is on a new row by comparing an element's .top value against the previous element's .top value.

 

So you would end up with an array of arrays.


const grid = [
  [element, element, element],
  [element, element, element],
  [element, element]
];

// inside a loop
const row = grid[i];
const element = row[0]; // first element in row
element.myAnimation = new TimelineMax({ paused: true })
  .staggerFromTo(row, 1, {}, {});

 

Does that make any sense? I might be able to make a demo of how to do that later if you need more help.

 

Trying to follow your logic a bit, I made this line of code:

 

jQuery(document).ready(function(){
    const config = {
        root: null,  
        rootMargin: '5px',
        threshold: 0
    };
    const top = document.querySelector('.uk-grid > div');
    const observer = new IntersectionObserver(monitoraScroll, config);
    const vetorElementos = [].slice.call(top);    
    for(var i=0; i < vetorElementos.lenght; i++){
        const temp = vetorElementos[i];
        temp.myAnimation = new TimelineMax({ paused: true }).staggerFromTo(i, .3, {autoAlpha: 0, y: 150},{autoAlpha: 1, y: 0}, .3);   
        observer.observe(temp);
    }  
    function monitoraScroll(top, observe) {
        for (var entry of top) {
            if (entry.isIntersecting) {
                entry.target.myAnimation.play();
            }
        }
    }
});

 

I have the same result as the demo code I posted (nothing happens).
I don't know if it's correct.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...