Make the jQuery Nivo Slider Stop after One Loop

Written by on October 14, 2010 in jQuery - 19 Comments
nivo4

One of the most common jQuery sliders being used by designers and developers these days is the Nivo Slider. I have personally used it on a variety of website for different clients. The main reasons I use it are the simplicity of the installation, how quickly you can change effects, and of course because it works on iPhones and iPads.

As versatile as the slider is, I noticed one important feature it’s missing; the lack of a way to shut off the auto loop. By this I mean that you may want it to go through a set of images once, and stop at the last image. It’s strange there isn’t a simple setting built in, but I have two solutions here for this issue.

For both methods I’m providing a zip file with the entire demo for your download.

Method 1.

In this first method the Nivo Slider starts and ends on the same image (slide 1).

View Demo
Download Files

The following javascipt code goes in your head tag, the slider has an ID of “slider“.

$(window).load(function() {
	$('#slider').nivoSlider({
		startSlide:0, //Set starting Slide (0 index)
		slideshowEnd: function(){$('#slider').data('nivo:vars').stop = true;} 
	});
});

Method 2.

In this second method the Nivo Slider only plays each image once.
Because of the way the slider works it always must end on slide 1. So what I did is force it to start on slide 2. So when it stops on slide 1 it has only played each image once. Put whatever image you want to be first in the slider as Slide 2. A little quirky, but hey..it works!

View Demo
Download Files

The following javascript code goes in your head tag, the slider has an ID of “slider“.

$(window).load(function() {
	$('#slider').nivoSlider({
		startSlide:1, //Set starting Slide (0 index)
		slideshowEnd: function(){$('#slider').data('nivo:vars').stop = true;} 
	});
});

Sponsors

About the Author

Mike Ilsley "ilz" is a Boston based website designer, front-end developer, WordPress addict, owner of Beantown Design, and exclusive ThemeForest author. Follow him on Twitter and Facebook, and don't forget to subscribe to the WebDesign&Such email RSS feed. Also, check out his latest project, jQueryMobileShowcase.com.


  • Maanu

    Thanks,

    Another great tutorial and demo for my website. Just what I needed.. Awesome!!!!!!!!!!

  • Chasez

    Hi,

    I saw your demo and noticed that the navigation arrows was changed. That’s what I’m looking for.

    I had changed the code as follow but the image of the nav arrows just can’t changed.

    The code below is put on the blogspot HTML/JavaScript gadget.

    *Before:
    background:url(http://nivo.dev7studios.com/images/arrows.png) no-repeat;
    text-indent:-9999px;
    border:0;
    }

    *After (I uploaded my desire arrows images into my hosting):
    background:url(http://www.mydomainname.com/images/arrows.png) no-repeat;
    text-indent:-9999px;
    border:0;
    }

    Any suggestion? Thanks.

    • ilz

      My first guess would be that your style sheet isn’t actually hooked up to your page with the slider. If your CSS style sheet is calling out custom images you should be all set. Do you have a link that you could post here so I could take a look?

    • Chasez

      I know the problem in not form the blogspot HTML/JavaScript. It’s from nivo-slider.css.

      But still cant get it right.

      http://eslene-marketing.blogspot.com/

      • ilz

        I took a look at your code. The CSS is being added to your page which calls out to the Nivo arrows. Are you using some sort of a plugin to install the nivo slider on your website?

        Where did you put the CSS code above..in your external CSS file? The first thing I would try is to put adding “!important” after your CSS rule to see if that will over-ride the styles being added to your head.

        So try this:

        background:url(http://www.mydomainname.com/images/arrows.png) no-repeat !important;
        text-indent:-9999px;
        border:0;
        }

        • Chasez

          The arrows appear but the Next arrows is same as Prev arrows. Where should I correct?

        • Chasez

          Thanks for the help. Solved.

  • Bruce

    Thanks. Method 2 worked like a charm. I just this functionality was built in so this workaround isn’t necessary.

    Thanks again!

  • http://littlegreenweb.com.au Tzaddi

    Thanks Mike, I used option two with great success on a clients wordpress site.
    I made sure to change the $’s to jQuery though to avoid possible conflicts with other wordpress plugins etc as per the recommendations on the nivo slider home site http://dev7studios.com/blog/resources/setting-up-the-nivo-slider-in-wordpress/
    Cheers, and thanks again. Very useful.

  • Josh

    I was wondering if you knew a way to make the startSlide random so that it wouldn’t start on the same slide every time, that way it would seem more dynamic and not like a flash player. Any suggestions?

  • http://www.codecraft.co.uk Chris

    Here is a simple solution for returning the slider to the first slide and stopping after one loop:

    $(window).load(function() {

    var oneLoop = false;

    $(‘#slider’).nivoSlider({

    [PUT YOUR CONFIG HERE]

    afterChange: function(){ if (oneLoop) { $(‘#slider’).data(‘nivoslider’).stop(); } },
    slideshowEnd: function(){ oneLoop = true },

    });
    });

    In a nutshell, when the slide show ends, it sets a variable internally, but does nothing else. Then the ‘afterChange’ event fires when the slide changes again (back to the first one) and, seeing that the variable is set, stops the slider.

  • http://www.greenbar.biz Darryn Cooke

    Great little tip. I love the Nivo Slider for its versatility and have been looking for a way to enhance its capabilities. Great piece of add on code.

  • jepkun

    I your code works great! but is it possible to make it stop on the last image/slide and not after a loop?

    • http://www.codecraft.co.uk Chris

      I’ve not tried it, but it should work by just stopping the show at the end, rather than letting it slide one more time.

      iirc, slideShowEnd is fired when the last slide appears, not when it disappears, so it should be the same as above except remove ‘afterChange’ as its not needed, and change slideshowEnd to:

      slideshowEnd: function(){
      $(‘#slider’).data(‘nivoslider’).stop();
      }

      Good luck!

  • Tom Homer

    I was wondering if by any chance if these methods could be used on Start/Stop slider by Chris Coyier shown here http://css-tricks.com/startstop-slider ,

    I have a project that I am working on that uses this slider. The client loves it, but now wants it to to just cycle through once and stop. Also after it cycles and stops the “Stop” button to turn to “Start” so that if the user wants to play it again they can.

    I am a newbie at jquery and am having no luck figuring this out.

    Thank you for your help

    • http://www.codecraft.co.uk Chris

      Different sliders work in different ways, i’m not familiar with the one you posted.

      I’d suggest heading over to somewhere like stackoverflow.com and asking your question there, more people will see it there than here and you’re far more likely to get the answers you’re looking for.

      Good luck :)

  • http://fairviewdesign.com susan

    Hi – Lovely work.. I am trying to embed links on the final image..

    tried an image map – it caused the javascript to break.

    now I set up absolutely positioned divs which hold the hyperlinks which works in IE7 but not in firefox..

    could you take a look and see if you can help me figure out how to make this work?

    url: http://sovanabistro.com/new4/catering2/

    the positioned div’s are outline in red..

    thanks, Sue

    • http://fairviewdesign.com susan

      sorry for the bother – this is working now.

      • mike ilz

        Susan,

        I checked out your link. I really like your usage of the slider to make an almost “video” type of effect. Neat stuff, I’m going to keep that in mind for future sites.

        Another way you could have made the links was to use live text in a nivo slider “htmlcaption”.. then the links would be search engine friendly as well.