首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery.countdown样式

jQuery.countdown样式
EN

Stack Overflow用户
提问于 2015-04-16 16:00:15
回答 2查看 2.7K关注 0票数 2

我已经用http://hilios.github.io/jQuery.countdown/创建了一个jQuery倒计时器

代码语言:javascript
复制
<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

但是,我是否设计了倒计时的样式,以达到上述主页的倒计时效果?

EN

回答 2

Stack Overflow用户

发布于 2015-04-16 16:17:37

通过查看主页的源代码,我设法将类似的东西“hack”在一起:http://plnkr.co/edit/2UwnSfidZHWDt1eg9P4n?p=preview

这里面有很多内联JS:

代码语言:javascript
复制
 $(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);
        });
      }
    });
  });
票数 4
EN

Stack Overflow用户

发布于 2017-01-11 21:17:32

只需在strftime函数中添加div,就可以按您想要的方式设置样式:

代码语言:javascript
复制
$('#getting-started').countdown('2025/02/25', 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'));
});
代码语言:javascript
复制
.test{
color:green;
}
代码语言:javascript
复制
<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>

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29668725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档