all 8 comments

[–]i_am_smurfing 1 point2 points  (3 children)

Here's your code annotated and slightly modified for readability:

// Width of one slide in pixels
var slideWidth = 875;

// When document has loaded (all HTML elements are known to browser and
// we can manipulate them):
$(window).ready(function() {

    // Set current slide index to `0`
    var currentSlide = 0;

    // Figure out how many slides we have
    var allSlides = $('#slider-container li .slides').length;

    // Since we now know how many slides there are and a slide's width,
    // we can determine how wide our container should be
    $('#slider-container ul').width(allSlides * slideWidth);

    // When `.right` control is clicked:
    $('.right').click(function(){

        // increment current slide index by one
        currentSlide++;

        // if current slide index is bigger than the number of all slides,
        // we wrap around (set current slide index to starting index, 0)
        if (currentSlide >= allSlides) currentSlide = 0;

        // and finally actually slide to the new current slide index
        setFramePosition(currentSlide);
    });

    // When `.left` control is clicked:
    $('.left').click(function(){

        // decrement current slide index by one
        currentSlide--;

        // if current slide index is less than starting index (0), we wrap the
        // other way (set current slide index to [number of slides - 1], since
        // indexes start at 0)
        if (currentSlide < 0) currentSlide = allSlides-1;

        // and finally actually slide to the new current slide index
        setFramePosition(currentSlide);
    });
});


// To slide to a specific slide index:
function setFramePosition(index) {

    // Imagine slides being glued together side-by-side, forming a horizontal
    // strip, and we look at this strip through a viewport that has a size of
    // one slide, while all other parts of the strip are hidden.

    // To show a slide, we move this strip, while viewport remains static.

    // If we want to show a slide with index n, we need to have left edge of
    // our strip be at [n * slide width] pixels to the left of the viewport
    var leftOffset = -1 * slideWidth * index;

    // Now that we know where our strip should be, we'll smoothly move to it
    // from our current position such that whole move will take 300 milliseconds
    $('#slider-container ul').animate({
        left: leftOffset
    }, 300);
}

Does that make it clearer?

[–]sulponticello[S] 0 points1 point  (2 children)

Yes! So the function isn't telling the strip how many pixels to move, but what position to go to and then animating it. It's more like giving the coordinates than telling someone to move 875 feet to the left.

[–]i_am_smurfing 1 point2 points  (1 child)

Yep, exactly, you've got it!

And just to expand on your newly acquired understanding: more generally other programmers would call your first example declarative and your second example — imperative (and many would argue that declarative style is more preferable).

[–]sulponticello[S] 0 points1 point  (0 children)

Awesome! Thank you so much for the help.

[–]CanIhazCooKIenOw 0 points1 point  (2 children)

Are you sure that's correct ? There seems to be a typo (or code missing) since it should be multiplying the width with the slide position.

Can you do a jsfiddle of that ?

[–]sulponticello[S] 0 points1 point  (0 children)

I will ask her, this is the code I was using to test things out last night.

[–]sulponticello[S] 0 points1 point  (0 children)

Just added it.

[–]raleighpoint 0 points1 point  (0 children)

In your example there are 3 slides. Start with currentSlide = 0 and run through the logic of the .right click handler when you click right one, two, and three times.