Skip to content

Slideshow exercise counters aren't updated in manual mode #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rmurphey opened this issue Jun 30, 2011 · 2 comments
Closed

Slideshow exercise counters aren't updated in manual mode #12

rmurphey opened this issue Jun 30, 2011 · 2 comments

Comments

@rmurphey
Copy link
Contributor

Via Abdul Popoola, the counters in the slideshow exercise do not update when manipulating the slideshow manually. His proposed fix is below:

/*

Move the #slideshow element to the top of #main.

Write code to cycle through the list items inside the element;
fade one in, display it for a few seconds, then fade it out and
fade in the next one.

When you get to the end of the list, start again at the beginning.

Add a counter under the slideshow (2 of 3, etc.) $.fn.prevAll

Add buttons under the counter that let you take manual control
of the slideshow and stop the automatic animation.

*/


$('h1').html('new html').attr()


$(document).ready(function() {
    var timeout, manualMode = false,

        $slideshow = $('#slideshow').prependTo('#main'),

        $counter = $('<div/>').insertAfter($slideshow),

        $controls = $('<div/>').insertAfter($slideshow),

        $prevBtn = $('<input/>', {
            type : 'button',
            value : 'previous'
        }).appendTo($controls),

        $nextBtn = $('<input/>', {
            type : 'button',
            value : 'next'
        }).appendTo($controls),

        $items = $slideshow.find('li').hide(),

        total = $items.length,

        updateCounter = function(num) {
            $counter.text(num + ' of ' + total);
        },

        getItem = function($item, trav) {
            var $returnItem = $item[trav]();
            return $returnItem.length ?
                $returnItem :
                $items[(trav == 'next') ? 'first' : 'last']();
        },

        showItem = function($currentItem, $itemToShow) {
            var $itemToShow =
                $itemToShow || getItem($currentItem,'next');

            $currentItem.fadeOut(500, function() {
                $itemToShow.fadeIn(500, fadeCallback);
            });

        },

        fadeCallback = function() {


            var $this = $(this),

                num = $this.prevAll().length + 1;

            // update the counter
            updateCounter(num);
                        if (manualMode) { return; }
                        $next = getItem($this, 'next'),
            // set the timeout for showing
            // the next item in 5 seconds
            timeout = setTimeout(function() {
                showItem($this, $next);
            }, 5000);
        },

        handleBtnClick = function(e) {
            clearTimeout(timeout);

            manualMode = true;


            var $currentItem = $items.filter(':visible'),
                $itemToShow = getItem($currentItem, e.data.direction);

            showItem($currentItem, $itemToShow);
        };

    $prevBtn.bind('click', { direction : 'prev' }, handleBtnClick);
    $nextBtn.bind('click', { direction : 'next' }, handleBtnClick);

    $items.eq(0).fadeIn(500, fadeCallback);
});
@addyosmani
Copy link
Member

No problem updating, but a quick general question for all: with the new site are we going to maintain all of the jQfundamentals exercises that were in place with the book version of the site? Be nice to have them, but was unsure whether a decision was made otherwise.

@johnkpaul
Copy link
Contributor

I am closing this issue because it's been decided that learn will not have exercises. More explanation here #30 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants