javascript - 使用计时器滑动到轮播中的最后一张幻灯片 - slick.js

标签 javascript jquery html css slick.js

我的网页上有以下代码行 - example/demo .

HTML:

<div class="qa">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

<p class="countdown-timer">00:00:15</p>

JavaScript/jQuery:

/* ----- Slick ----- */
$(document).ready(function() {
  $('.qa').slick({
    infinite: false,
    slidesToShow: 1,
    slidesToScroll: 1,
    adaptiveHeight: false,
    arrows: true,
    mobileFirst: true,
    respondTo: 'window',
    useCSS: true,
    swipeToSlide: false
  });
});

/* ----- Timer ----- */
function Stopwatch(config) {
  // If no config is passed, create an empty set
  config = config || {};
  // Set the options (passed or default)
  this.element = config.element || {};
  this.previousTime = config.previousTime || new Date().getTime();
  this.paused = config.paused && true;
  this.elapsed = config.elapsed || 0;
  this.countingUp = config.countingUp && true;
  this.timeLimit = config.timeLimit || (this.countingUp ? 60 * 10 : 0);
  this.updateRate = config.updateRate || 100;
  this.onTimeUp = config.onTimeUp || function() {
    this.stop();
  };
  this.onTimeUpdate = config.onTimeUpdate || function() {
    console.log(this.elapsed)
  };
  if (!this.paused) {
    this.start();
  }
}

Stopwatch.prototype.start = function() {
  // Unlock the timer
  this.paused = false;
  // Update the current time
  this.previousTime = new Date().getTime();
  // Launch the counter
  this.keepCounting();
};

Stopwatch.prototype.keepCounting = function() {
  // Lock the timer if paused
  if (this.paused) {
    return true;
  }
  // Get the current time
  var now = new Date().getTime();
  // Calculate the time difference from last check and add/substract it to 'elapsed'
  var diff = (now - this.previousTime);
  if (!this.countingUp) {
    diff = -diff;
  }
  this.elapsed = this.elapsed + diff;
  // Update the time
  this.previousTime = now;
  // Execute the callback for the update
  this.onTimeUpdate();
  // If we hit the time limit, stop and execute the callback for time up
  if ((this.elapsed >= this.timeLimit && this.countingUp) || (this.elapsed <= this.timeLimit && !this.countingUp)) {
    this.stop();
    this.onTimeUp();
    return true;
  }
  // Execute that again in 'updateRate' milliseconds
  var that = this;
  setTimeout(function() {
    that.keepCounting();
  }, this.updateRate);
};

Stopwatch.prototype.stop = function() {
  // Change the status
  this.paused = true;
};

$(document).ready(function() {
  /*
   * First example, producing 2 identical counters (countdowns)
   */
  $('.countdown-timer').each(function() {
    var stopwatch = new Stopwatch({
      'element': $(this), // DOM element
      'paused': false, // Status
      'elapsed': 1000 * 1 * 15, // Current time in milliseconds
      'countingUp': false, // Counting up or down
      'timeLimit': 0, // Time limit in milliseconds
      'updateRate': 100, // Update rate, in milliseconds
      'onTimeUp': function() { // onTimeUp callback
        this.stop();
        $(this.element).html('Times Up');

        /* $(".qa").slick('slickGoTo', 5); */       

      },
      'onTimeUpdate': function() { // onTimeUpdate callback
        var t = this.elapsed,
          h = ('0' + Math.floor(t / 3600000)).slice(-2),
          m = ('0' + Math.floor(t % 3600000 / 60000)).slice(-2),
          s = ('0' + Math.floor(t % 60000 / 1000)).slice(-2);
        var formattedTime = h + ':' + m + ':' + s;
        $(this.element).html(formattedTime);
      }
    });
  });
});

我怎样才能让它在倒数计时器达到 0 时滑动到轮播中的最后一张幻灯片?

目前我正在使用下面的行来实现这一点,但这不是一个可行的解决方案,因为该脚本在不同页面上被多次使用,并且可能会在稍后阶段添加更多幻灯片:

$(".qa").slick('slickGoTo', 5);

最佳答案

这样的事情怎么样

$(".qa").slick('slickGoTo', $('.qa div').length);

或者更简洁的代码

$(".qa").slick('slickGoTo', $('.qa').children('div').length);

关于javascript - 使用计时器滑动到轮播中的最后一张幻灯片 - slick.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31491551/

相关文章:

javascript - 函数执行完才再次调用函数

html - 框阴影在 HTML、CSS 中无法正常工作

javascript - 简单地将图像转换为 Canvas 不起作用

javascript - 如何在悬停时弹出图像?

javascript - jQuery UI 对话框 : Close when Click Outside

jquery - 为什么这个 JSON 无效?

javascript - PHP Javascript 更改浏览器后退按钮行为 Laravel

html - 文本以容器的最小高度和调整高度为中心

html - div(平铺)的百分比宽度

javascript - 从内部函数访问本地对象属性(Javascript)