jQuery.倒计时样式

标签 jquery css twitter-bootstrap

我使用以下命令创建了一个 jQuery 倒计时器:http://hilios.github.io/jQuery.countdown/

<h1>Count down timer</h1>

  <div id="getting-started"></div>

  <script type="text/javascript">
    $('#getting-started').countdown('2016/01/01', function(event) {
      $(this).html(event.strftime('%w weeks %d days %H:%M:%S'));
    });
  </script>

这是一个骗子:http://plnkr.co/edit/gcewqxLMchFmWgZjwpwW?p=preview

但是,我要不要设置倒计时的样式来实现上面首页的倒计时效果呢?

最佳答案

查看主页的源代码,我成功地“破解”了类似的内容:http://plnkr.co/edit/2UwnSfidZHWDt1eg9P4n?p=preview

里面有很多内联 JS:

 $(window).on('load', function() {
    var labels = ['weeks', 'days', 'hours', 'minutes', 'seconds'],
      nextYear = (new Date().getFullYear() + 1) + '/01/01',
      template = _.template($('#main-example-template').html()),
      currDate = '00:00:00:00:00',
      nextDate = '00:00:00:00:00',
      parser = /([0-9]{2})/gi,
      $example = $('#main-example');
    // Parse countdown string to an object
    function strfobj(str) {
      var parsed = str.match(parser),
        obj = {};
      labels.forEach(function(label, i) {
        obj[label] = parsed[i]
      });
      return obj;
    }
    // Return the time components that diffs
    function diff(obj1, obj2) {
      var diff = [];
      labels.forEach(function(key) {
        if (obj1[key] !== obj2[key]) {
          diff.push(key);
        }
      });
      return diff;
    }
    // Build the layout
    var initData = strfobj(currDate);
    labels.forEach(function(label, i) {
      $example.append(template({
        curr: initData[label],
        next: initData[label],
        label: label
      }));
    });
    // Starts the countdown
    $example.countdown(nextYear, function(event) {
      var newDate = event.strftime('%w:%d:%H:%M:%S'),
        data;
      if (newDate !== nextDate) {
        currDate = nextDate;
        nextDate = newDate;
        // Setup the data
        data = {
          'curr': strfobj(currDate),
          'next': strfobj(nextDate)
        };
        // Apply the new values to each node that changed
        diff(data.curr, data.next).forEach(function(label) {
          var selector = '.%s'.replace(/%s/, label),
              $node = $example.find(selector);
          // Update the node
          $node.removeClass('flip');
          $node.find('.curr').text(data.curr[label]);
          $node.find('.next').text(data.next[label]);
          // Wait for a repaint to then flip
          _.delay(function($node) {
            $node.addClass('flip');
          }, 50, $node);
        });
      }
    });
  });

关于jQuery.倒计时样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29668725/

相关文章:

html - 防止覆盖图像使底层图像不可点击

twitter-bootstrap - 理解Bootstrap的clearfix类

javascript - 如何根据日期和时间创建时间戳

jquery - 查找元素内没有类或 id 的元素 - jQuery

jquery - 如何找出 next() 何时到达末尾,然后转到第一项

html - 我怎样才能使图像适合

javascript - JS 配对游戏随机化除最后一张牌以外的所有牌

jquery - window.height() 不起作用?试图获取窗口的宽度

twitter-bootstrap - 如何在 Bootstrap 中有一个空的 col-md-*?

css - 使用 Bootstrap 4 固定导航栏在 HTML 中偏移滚动 anchor