Cycle 2 Next and Previous Button

1.1k views Asked by At

I have a cycle 2 js code that will change the chart every 15 seconds. However, how do I include a next/previous button so that user can click to any chart they want?

HTML code

<div id="chart1">
    <div id="SummaryLineChart"></div>
</div>

<div id="chart2">
    <div id="DailyBarChart"></div>
</div>

<div id="chart3">
    <div id="PieChart"></div>
</div>

My JS file:

jQuery(function () {
var $els = $('div[id^=chart]'),
    i = 0,
    len = $els.length;

$els.slice(1).hide();
setInterval(function () {
    $els.eq(i).fadeOut(function () {
        i = (i + 1) % len
        $els.eq(i).fadeIn();
    })
}, 15000)
})
1

There are 1 answers

6
errand On BEST ANSWER

first of all, we wrap your slideshow slides into a container and give every slide the same class (used as slide selector later on). then we also include a slide control div, with the previous and next elements. you can position them via css according to your needs.

<div id="slideContainer">
    <div class="slide" id="chart1">
        <div id="SummaryLineChart"></div>
    </div>

    <div class="slide" id="chart2">
        <div id="DailyBarChart"></div>
    </div>

    <div class="slide" id="chart3">
        <div id="PieChart"></div>
    </div>
    <div class="slideControls">
        <div id="prev">prev</div>
        <div id="next">next</div>
    </div>
</div>

my preferred infinite slideshow approach:

$("#slideContainer > .slide:gt(0)").hide(); //hide all slides, but the first

setInterval(function() {                   //start interval
    $('#slideContainer > .slide:first')    //select the first slide
      .fadeOut(1000)                       //fade it out
      .next()                              //select the next slide
      .fadeIn(1000)                        //fade it in
      .end()                               //end the current chain (first slide gets selected again)
      .appendTo('#slideContainer');        //move first slide to the end
    },  15000);

this creates an infinite slideshow, where the first slide is always the visible slide. this makes it a lot easier to address the current active slide...

your previous/next functions would look like that:

$("#next").on("click", function(){
    $('#slideContainer > .slide:first')    //select the first slide
      .fadeOut(1000)                       //fade it out
      .next()                              //select the next slide
      .fadeIn(1000)                        //fade it in
      .end()                               //end the current chain (first slide gets selected again)
      .appendTo('#slideContainer');        //move first slide to the end
});

$("#prev").on("click", function(){
    $('#slideContainer > .slide:first')    //select the first slide
      .fadeOut(1000);                      //fade it out        
    $('#slideContainer > .slide:last')    //select the last slide
      .fadeIn(1000)                        //fade it in
      .prependTo('#slideContainer');        //move last slide to the beginning
});