Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7620637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:59:03+00:00 2026-05-31T03:59:03+00:00

Here is a simple slider code and I wanted to understand how the variable

  • 0

Here is a simple slider code and I wanted to understand how the variable function closure is working…

(function($) {
var sliderUL = $('div.slider').css('overflow', 'hidden').children('ul'),
    imgs = sliderUL.find('img'),
    imgWidth = imgs[0].width, // 600
    imgsLen = imgs.length, // 4
    current = 1,
    totalImgsWidth = imgsLen * imgWidth; // 2400

$('#slider-nav').show().find('button').on('click', function() {
    var direction = $(this).data('dir'),
        loc = imgWidth; // 600

    // update current value
    **( direction === 'next' ) ? ++current : --current;
    // if first image
    if ( current === 0 ) {
        current = imgsLen;
        loc = totalImgsWidth - imgWidth; // 2400 - 600 = 1800
        direction = 'next';
    } else if ( current - 1 === imgsLen ) { // Are we at end? Should we reset?
        current = 1;
        loc = 0;
    }
    transition(sliderUL, loc, direction);**
});

function transition( container, loc, direction ) {
    var unit; // -= +=

    if ( direction && loc !== 0 ) {
        unit = ( direction === 'next' ) ? '-=' : '+=';
    }

    container.animate({
        'margin-left': unit ? (unit + loc) : loc
    });
}

})(jQuery);

In this part:

    $('#slider-nav').show().find('button').on('click', function() {
    var direction = $(this).data('dir'),
        loc = imgWidth; // 600

    // update current value
    ( direction === 'next' ) ? ++current : --current;

    // if first image
    if ( current === 0 ) {
        current = imgsLen;
        loc = totalImgsWidth - imgWidth; // 2400 - 600 = 1800
        direction = 'next';
    } else if ( current - 1 === imgsLen ) { // Are we at end? Should we reset?
        current = 1;
        loc = 0;
    }

    transition(sliderUL, loc, direction);
});

In the if ( current === 0) block, are the variables current, loc, and direction updated after being changed in the first block, then they are passed to the transition function below?

I take it that if the else if ( current – 1 === imgsLen ) is true, then current and loc are write over the previous values assigned to them, to then be passed into the transitions function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:59:05+00:00Added an answer on May 31, 2026 at 3:59 am

    direction and loc are defined in the scope of your callback function for ‘click’. They are initialized every time you call that callback function — at every click. direction is initialized by the ‘data-dir’ associated to the button and loc is initialized to the current value of imgWidth (which is a variable defined in the scope of your main function).

    After being initialized, direction and loc can be modify by your if-else if logic and they are then passed to the function transition(). (Note that sliderIU is also passed to transition(), but it is not necessary since it has the scope of your main function and it could be used directly within transition() without being passed as parameter container. However it might not be a bad idea to pass it as a parameter since it makes for clearer programming style when you can see from the arguments of a function on which values it is acting.)

    The variable current which is used in the callback-click function is not defined in the scope of that callback-click function but has the main function scope (defined at the start of the main function). This is necessary because you want to change its value –and remember the change– each time the button is clicked. The callback-click function is a closure since it has access (read+write) to the external variable current. The variables direction and loc have nothing to do with closures; they are just variables that you pass to a function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is whats happening (Code Block Below): I am working on a simple drop-down
I have some content sliding here. http://www.smallsharptools.com/downloads/jQuery/Slider/slider.html The HTML structure is simple. There is
my sample code is here include 'simple_html_dom.php'; function get_all_links($url){ global $host; $html = new
Hi i've got here a simple program, but it's not working properly. When i
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here is the code I use to attach the div: $(#sessions).prepend(tempct); $(#sessions>div:first-child).hide().slideDown(1000).delay(100,function(){ $(#sessions>div:first-child).click(); $(#renamemeeting).click();
Simple Wordpress problem - get_post_meta is not retrieving custom field values. Here's the code
I am trying to create a simple JQuery Div Slider, but it doesn't seem
Here is a simple code to animate webview screen. But the problem is, the
Please excuse me if I'm simple here, I want to create a simple widget

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.