jQuery.countdown样式

时间:2015-04-16 08:00:15

标签: jquery css css3 twitter-bootstrap

我使用:http://hilios.github.io/jQuery.countdown/

创建了一个jQuery倒数计时器
<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

但是,我是否设定了倒计时以实现上述主页的倒计时效果?

2 个答案:

答案 0 :(得分:2)

查看主页的来源,我设法成功了解了主页。类似的东西: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);
        });
      }
    });
  });

答案 1 :(得分:1)

你可以通过在strftime函数中添加div来按照你想要的方式进行样式化:

&#13;
&#13;
$('#getting-started').countdown('2019/02/19', function(event) {
    var $this = $(this).html(event.strftime(''
    + '<div class="test">%w</div> weeks '
    + '<div class="test">%d</div> days '
    + '<div class="test">%H</div> hr '
    + '<div class="test">%M</div> min '
    + '<div class="test">%S</div> sec'));
});
&#13;
.test{
color:green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.rawgit.com/hilios/jQuery.countdown/2.2.0/dist/jquery.countdown.min.js"></script>

<nav><div id="getting-started"></ul></nav>
&#13;
&#13;
&#13;