【问题标题】:Cycle 2 Next and Previous Button循环 2 下一个和上一个按钮
【发布时间】:2014-11-20 06:47:32
【问题描述】:

我有一个周期 2 js 代码,它将每 15 秒更改一次图表。但是,如何包含下一个/上一个按钮,以便用户可以点击他们想要的任何图表?

HTML 代码

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

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

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

我的 JS 文件:

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)
})

【问题讨论】:

标签: javascript jquery cycle jquery-cycle


【解决方案1】:

首先,我们将您的幻灯片包装到一个容器中,并为每张幻灯片提供相同的类(稍后用作幻灯片选择器)。然后我们还包括一个滑动控件 div,带有上一个和下一个元素。您可以根据需要通过 css 定位它们。

<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>

我首选的无限幻灯片方法:

$("#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);

这将创建一个无限幻灯片,其中第一张幻灯片始终是可见幻灯片。这使得处理当前活动幻灯片变得更加容易......

您的上一个/下一个函数如下所示:

$("#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
});

【讨论】:

  • 嗨,它适用于无限幻灯片,但是当我添加上一个/下一个功能时,它会显示页面上的所有图表
  • 是的,我的错。错过了点击功能中分号之前的结束“)”。纠正它。我想我需要先喝杯咖啡……
  • 我还建议将控件移出容器。不知道 .next() 是否会留在它的选择器 .slide 中,或者它是否还会选择控件 div...
  • 谢谢它的工作原理:) 但有一件事,当我点击太多下一步时,它会显示一个空框架。知道如何解决它吗? jsfiddle.net/muz8545g
  • 哦,等等,没有阅读您的其他评论来移动控件..它现在修复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多