最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery image slider loop with setInterval - Stack Overflow

programmeradmin6浏览0评论

I have this markup:

<div id="imagedisplay">
    <div class="slider_item active"></div>
    <div class="slider_item"></div>
    <div class="slider_item"></div>
    <div class="slider_item last"></div>
</div>

and this script

setInterval(function(){
    $('.slider_item.active').fadeOut().removeClass('active')
        .next('.slider_item').fadeIn().addClass('active');
}, 5000);

How do I make this loop in the best way? Right now the last image just fades out, without the first image fading in again.

I have this markup:

<div id="imagedisplay">
    <div class="slider_item active"></div>
    <div class="slider_item"></div>
    <div class="slider_item"></div>
    <div class="slider_item last"></div>
</div>

and this script

setInterval(function(){
    $('.slider_item.active').fadeOut().removeClass('active')
        .next('.slider_item').fadeIn().addClass('active');
}, 5000);

How do I make this loop in the best way? Right now the last image just fades out, without the first image fading in again.

Share Improve this question edited Sep 6, 2012 at 15:28 Danil Speransky 30.5k6 gold badges69 silver badges78 bronze badges asked Sep 6, 2012 at 13:30 afcdesignafcdesign 3672 gold badges6 silver badges16 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Check if nextItem has items and if not set it back to the first:

var nextItem = $('.slider_item.active')
    .fadeOut()
    .removeClass('active')
    .next('.slider_item');

if (nextItem.length === 0) {
   nextItem = $('.slider_item').first();
}

nextItem.fadeIn().addClass('active');

Here's an example: jsFiddle

Demo: http://jsfiddle/gdS8Q/2/

var cur = 0;
var count = $('.slider_item').length;

$('.slider_item').hide();
$('.slider_item').eq(0).show();

setInterval(function() {    
    $('.slider_item').eq(cur).fadeOut(function () {
        $(this).removeClass('active');
        cur = (cur + 1) % count;
        $('.slider_item').eq(cur).addClass('active').fadeIn();
    });
}, 2000);​

I actually just wrote a plugin for this and it's pretty readable so you can probably understand by just looking at it.

Github repo - https://github./joshbedo/fullPageSlider/blob/master/fullpagesliderV2.js

Some of the functionality for arrows is still being worked on to be more dynamic but it works here is an example as well. I'm sliding html but you can easily just add a .slide-panel class with images inside.

In action http://strikingalchemy.publishpath./portfolio

EDIT: They edited the script so it doesn't have a setInterval loop the original on github has a interval loop once the user clicks the arrow and is interested to see more. Easily changeable if you want it to loop on load.. I just didn't want to use extra resources when the user wasn't interested. Going to put an example on github later after work.

I know it is bumping an old thread, but can someone tell me the correct code to display the previous div?

This is the HTML

<div id="imagedisplay">
    <div class="slider_item active"></div>
    <div class="slider_item"></div>
    <div class="slider_item"></div>
    <div class="slider_item last"></div>
</div>

and the jQuery

     var nextItem = $('.slider_item.active')
        .fadeOut()
        .removeClass('active')
       .next('.slider_item');

 if (nextItem.length === 0) {
       nextItem = $('.slider_item').first();
    }
       nextItem.fadeIn().addClass('active');

I saw the jsfiddle link and tried various bination but cant seem to grasp the correct code

Thanks

发布评论

评论列表(0)

  1. 暂无评论