Jump to content

Two (more) moronic questions about Javascript scope

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:




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


Share this post

Link to post
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.



    var $element = $(el),
        pan_thumb = new TimelineMax({paused:true,repeat:-1});
    // store timeline in el
    el.tl = 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 });



More info on this technique can be found here:



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! :)

Share this post

Link to post
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

Share this post

Link to post
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.




Share this post

Link to post
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( Integer index, Element element )




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! :)

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.