Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
jasper

Two (more) moronic questions about Javascript scope

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

I want to animate something (the position of the background image) of a div when the user hovers over (mouseovers) it. There will be several of these divs on a single page.  It would be REALLY nice to be able to specify classes, not IDs, of DIVs, when constructing greensock timelines, but as that's impossible I've had a stab at it in this jsfiddle:

 

http://jsfiddle.net/cheolsoo/mp3qes6n/

 

1/ why doesn't the (global) value of i get set when the mouseover event fires?  I initialized i to 611 but it doesn't change when I mouseover one of the other divs.

2/ (much less critical as I can work around it) my fiddle works (sort of) because I stripped "vid_" away from the div ID and then added it back in the TimelineMax definition.  I tried simply passing a variable (e.g. i where i='vid_611' rather than '611') instead of "vid_"+i, but then nothing worked at all.  Why not?  (Is that a greensock question or a Javascript question?  I'm such a moron I'm not even sure.)

thanks, eh
 

Ryan

Link to comment
Share on other sites

Hello jasper and welcome to the GreenSock Forum!

 

Just run your timeline and animation in a for loop or jQuery each() method to apply each new timeline for your classes. And then store your timeline in the element.

 

http://jsfiddle.net/mp3qes6n/10/

$(".list_thumb").each(function(i,el){
    
    var $element = $(el),
        pan_thumb = new TimelineMax({paused:true,repeat:-1});
    
    // store timeline in el
    el.tl = pan_thumb;
    
    pan_thumb
        .to($element, .4, { 'backgroundPosition':'0% 0%', ease:Power1.easeOut })
        .to($element, .4, { 'backgroundPosition':'0% 0%', ease:Power1.easeOut })
        .to($element, .8, { 'backgroundPosition':'100% 0%' })
        .to($element, .8, { 'backgroundPosition':'0% 100%' })
        .to($element, .8, { 'backgroundPosition':'100% 100%' })
        .to($element, .4, { 'backgroundPosition':'50% 50%', ease:Power1.easeIn });
    
});
    
$('.list_thumb').mouseover(function(){
    this.tl.play();
});

$('.list_thumb').mouseout(function(){
    this.tl.reverse();
});

:

More info on this technique can be found here:

 

https://greensock.com/forums/topic/10447-best-practice-for-kllingundoing-timelines-of-objects-on-rolloverout/

See the Pen yLeIt?editors=011 by rhernando (@rhernando) on CodePen

See the Pen vjGxH by rhernando (@rhernando) on CodePen

See the Pen itmeB by jonathan (@jonathan) on CodePen

 

Hope this helps! :)

Link to comment
Share on other sites

Thank you sir, and thank you for the kind "welcome" to the forum! (My tenth post actually; not sure why this one doesn't show up on my list of posts.)

 

I'll give it a whirl. :D

Link to comment
Share on other sites

Another question if I might jonathan:

 

What is the purpose of "i" passed to the function in the first line? It's in all of the codepens you provide, but not spelled out anywhere, or at http://api.jquery.com/each/ either for that matter.

 

thanks,

Ryan

Link to comment
Share on other sites

basically the i just represents the index of each element that is iterated in the loop. I just gave it a name of 'i' to represent the index. Those are just arguments that each() has defined to get passed through the anonymous function, so you can name them whatever you want.

 

$( "li" ).each(function( index, element ) {

console.log( index );

console.log( element );

});

 

Taken from your above link to jQuery each() :

 

The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0.

function

 

function( Integer index, Element element )

 

http://api.jquery.com/each/

 

The above code could have also been done like this in a for loop:

 

var element = document.getElementsByTagName("li");

for (var i = element.length - 1; i >= 0; i--){

console.log( i );

console.log( element );

}

 

Hope that helps! :)

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.
×