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 7908633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:02:15+00:00 2026-06-03T12:02:15+00:00

I wrote a slideshow plugin, but for some reason maybe because I’ve been working

  • 0

I wrote a slideshow plugin, but for some reason maybe because I’ve been working on it all day, I can’t figure out exactly how to get it to go back to state one, once it’s reached the very last state when it’s on auto mode.

I’m thinking it’s an architectual issue at this point, because basically I’m attaching the amount to scroll left to (negatively) for each panel (a panel contains 4 images which is what is currently shown to the user). The first tab should get: 0, the second 680, the third, 1360, etc. This is just done by calculating the width of the 4 images plus the padding.

I have it on a setTimeout(function(){}) currently to automatically move it which works pretty well (unless you also click tabs, but that’s another issue). I just want to make it so when it’s at the last state (numTabs – 1), to animate and move its state back to the first one.

Code:

(function($) {
var methods = {
init: function(options) {
var settings = $.extend({
‘speed’: ‘1000’,
‘interval’: ‘1000’,
‘auto’: ‘on’
}, options);

        return this.each(function() {
            var $wrapper = $(this);
            var $sliderContainer = $wrapper.find('.js-slider-container');
            $sliderContainer.hide().fadeIn();

            var $tabs = $wrapper.find('.js-slider-tabs li a');
            var numTabs = $tabs.size();
            var innerWidth = $wrapper.find('.js-slider-container').width();

            var $elements = $wrapper.find('.js-slider-container a');
            var $firstElement = $elements.first();
            var containerHeight = $firstElement.height();
            $sliderContainer.height(containerHeight);

            // Loop through each list element in `.js-slider-tabs` and add the
            // distance to move for each "panel". A panel in this example is 4 images
            $tabs.each(function(i) {
                // Set amount to scroll for each tab
                if (i === 1) {
                    $(this).attr('data-to-move', innerWidth + 20); // 20 is the padding between elements
                } else {
                    $(this).attr('data-to-move', innerWidth * (i) + (i * 20));
                }

            });

            // If they hovered on the panel, add paused to the data attribute
            $('.js-slider-container').hover(function() {
                $sliderContainer.attr('data-paused', true);
            }, function() {
                $sliderContainer.attr('data-paused', false);
            });

            // Start the auto slide
            if (settings.auto === 'on') {
                methods.auto($tabs, settings, $sliderContainer);
            }

            $tabs.click(function() {
                var $tab = $(this);
                var $panelNum = $(this).attr('data-slider-panel');
                var $amountToMove = $(this).attr('data-to-move');

                // Remove the active class of the `li` if it contains it
                $tabs.each(function() {
                    var $tab = $(this);
                    if ($tab.parent().hasClass('active')) {
                        $tab.parent().removeClass('active');
                    }
                });

                // Add active state to current tab
                $tab.parent().addClass('active');

                // Animate to panel position
                methods.animate($amountToMove, settings);
                return false;
            });
        });
    },

    auto: function($tabs, settings, $sliderContainer) {
        $tabs.each(function(i) {
            var $amountToMove = $(this).attr('data-to-move');

            setTimeout(function() {
                methods.animate($amountToMove, settings, i, $sliderContainer);
            }, i * settings.interval);
        });
    },

    animate: function($amountToMove, settings, i, $sliderContainer) {
            // Animate
            $('.js-slider-tabs li').eq(i - 1).removeClass('active');
            $('.js-slider-tabs li').eq(i).addClass('active');

            $('#js-to-move').animate({
                'left': -$amountToMove
            }, settings.speed, 'linear', function() {});
    }
};

$.fn.slider = function(method) {
    if (methods[method]) {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method) {
        return methods.init.apply(this, arguments);
    } else {
        return false;
    }
};
})(jQuery);

$(window).ready(function() {
    $('.js-slider').slider({
        'speed': '10000',
        'interval': '10000',
        'auto': 'on'
    });
});​

The auto and animate methods are where the magic happens. The parameters speed is how fast it’s animated and interval is how often, currently set at 10 seconds.

Can anyone help me figure out how to get this to “infinitely loop”, if you will?

Here is a JSFiddle

  • 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-06-03T12:02:20+00:00Added an answer on June 3, 2026 at 12:02 pm

    It would probably be better to let go of the .each() and setTimeout() combo and use just setInterval() instead. Using .each() naturally limits your loop to the length of your collection, so it’s better to use a looping mechanism that’s not, and that you can break at any point you choose.

    Besides, you can readily identify the current visible element by just checking for .active, from what I can see.

    You’d probably need something like this:

    setInterval(function () {
        // do this check here.
        // it saves you a function call and having to pass in $sliderContainer
        if ($sliderContainer.attr('data-paused') === 'true') { return; }
    
        // you really need to just pass in the settings object.
        // the current element you can identify (as mentioned),
        // and $amountToMove is derivable from that.
        methods.animate(settings);
    }, i * settings.interval);
    
    // ...
    
    // cache your slider tabs outside of the function
    // and just form a closure on that to speed up your manips
    var slidertabs = $('.js-slider-tabs');
    animate : function (settings) {
    
        // identify the current tab
        var current = slidertabs.find('li.active'),
    
        // and then do some magic to determine the next element in the loop
            next = current.next().length >= 0 ? 
                       current.next() : 
                       slidertabs.find('li:eq(0)')
            ;
    
        current.removeClass('active');
        next.addClass('active');
    
        // do your stuff
    
    };
    

    The code is not optimized, but I hope you see where I’m getting at here.

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

Sidebar

Related Questions

I've been using jQuery a long time and I've been writing a slideshow plugin
I've been having some issues with a pagination widget that I wrote in jQuery.
I wrote a program that forks some processes with fork(). I want to kill
I'm tryng to build a slide-show in my wordpress blog.I'm using jqTransitions plugin but
I wrote some code to automatically track many objects through an 1000+ frame image
I wrote a makefile which behaves oddly. You can find it here: http://pastebit.com/pastie/8215 Basically
I have a script that i wrote, but it prints the first part of
in web applicaiton,[asp.net] i am using modal popu control, it is working fine, but
I'm quite new to Flash and ActionScript. Anyway, I wrote my own XML Slideshow,
I've been messing around with this bit a javascript that I wrote from the

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.