当前位置:首页 > jquery

jquery倒计时

2026-02-03 09:07:10jquery

使用jQuery实现倒计时功能

倒计时功能在网页中常用于促销活动、限时抢购等场景。以下是几种常见的实现方式:

基础倒计时实现

function countDown(endTime, element) {
  const timer = setInterval(function() {
    const now = new Date().getTime();
    const distance = endTime - now;

    const days = Math.floor(distance / (1000 * 60 * 60 * 24));
    const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((distance % (1000 * 60)) / 1000);

    $(element).html(days + "天 " + hours + "时 " + minutes + "分 " + seconds + "秒 ");

    if (distance < 0) {
      clearInterval(timer);
      $(element).html("已过期");
    }
  }, 1000);
}

// 使用示例:设置一个24小时后的倒计时
const endDate = new Date();
endDate.setDate(endDate.getDate() + 1);
countDown(endDate, '#countdown');

可配置的倒计时插件

创建一个更灵活的倒计时插件:

jquery倒计时

(function($) {
  $.fn.countdown = function(options) {
    const settings = $.extend({
      endTime: new Date().getTime() + 86400000, // 默认24小时后
      onEnd: function() {},
      format: 'DD:HH:MM:SS'
    }, options);

    return this.each(function() {
      const element = $(this);
      const timer = setInterval(updateTimer, 1000);

      function updateTimer() {
        const now = new Date().getTime();
        const distance = settings.endTime - now;

        if (distance <= 0) {
          clearInterval(timer);
          settings.onEnd.call(element);
          return;
        }

        const days = Math.floor(distance / (1000 * 60 * 60 * 24));
        const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        const seconds = Math.floor((distance % (1000 * 60)) / 1000);

        let output = settings.format
          .replace('DD', days)
          .replace('HH', hours)
          .replace('MM', minutes)
          .replace('SS', seconds);

        element.html(output);
      }

      updateTimer();
    });
  };
}(jQuery));

// 使用示例
$('#countdown').countdown({
  endTime: new Date('2023-12-31').getTime(),
  format: '剩余: DD天HH小时MM分钟SS秒',
  onEnd: function() {
    $(this).html('活动已结束');
  }
});

天/小时/分钟/秒单独显示

如果需要单独控制每个时间单位的显示:

jquery倒计时

<div class="countdown">
  <span class="days"></span>天
  <span class="hours"></span>时
  <span class="minutes"></span>分
  <span class="seconds"></span>秒
</div>

<script>
$(document).ready(function() {
  const endDate = new Date();
  endDate.setHours(endDate.getHours() + 48); // 48小时后

  const timer = setInterval(function() {
    const now = new Date();
    const distance = endDate - now;

    if (distance <= 0) {
      clearInterval(timer);
      $('.countdown').html('时间到');
      return;
    }

    const days = Math.floor(distance / (1000 * 60 * 60 * 24));
    const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((distance % (1000 * 60)) / 1000);

    $('.days').text(days);
    $('.hours').text(hours);
    $('.minutes').text(minutes);
    $('.seconds').text(seconds);
  }, 1000);
});
</script>

带样式的倒计时

为倒计时添加CSS样式:

.countdown {
  font-family: Arial, sans-serif;
  font-size: 24px;
  color: #333;
  padding: 10px;
  background: #f5f5f5;
  border-radius: 5px;
  display: inline-block;
}

.countdown span {
  display: inline-block;
  min-width: 30px;
  text-align: center;
  padding: 5px;
  background: #fff;
  margin: 0 5px;
  border-radius: 3px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

服务器时间同步

为了避免客户端时间不准确的问题,可以从服务器获取时间:

$.get('/api/server-time', function(serverTime) {
  const endTime = new Date(serverTime);
  endTime.setHours(endTime.getHours() + 2); // 2小时后结束

  const timer = setInterval(function() {
    const now = new Date();
    const distance = endTime - now;

    // 倒计时逻辑...
  }, 1000);
});

以上方法提供了不同复杂度的jQuery倒计时实现,可以根据项目需求选择适合的方案。基础版本适合简单需求,插件版本更适合重复使用,服务器时间同步则解决了客户端时间不准确的问题。

标签: 倒计时jquery
分享给朋友:

相关文章

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,jQuery 以 DOM 操作和事件驱动为核心,而 Vue.js 是数据驱动的现代化框架。如果想用 jQuery 实现类似 Vue 的功能(如…

jquery库

jquery库

jQuery 库简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。其核心特点是“Write Less, Do More”,…

jquery 菜鸟

jquery 菜鸟

以下是针对 jQuery 初学者的基础指南,涵盖核心概念和常用操作: jQuery 简介 jQuery 是一个快速、轻量级的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和…

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,前者基于 DOM 操作,后者基于数据驱动。虽然 Vue 可以替代 jQuery 的大部分功能,但若需在 Vue 中临时集成 jQuery 或模…

jquery获取radio选中的值

jquery获取radio选中的值

获取radio选中的值 使用jQuery获取被选中的radio按钮的值可以通过以下几种方法实现: 方法1:使用:checked选择器 var selectedValue = $('input[na…

jquery 图表

jquery 图表

jQuery 图表库推荐 jQuery 本身不直接提供图表功能,但可以通过插件或集成其他库实现图表绘制。以下是几种常用的 jQuery 图表解决方案: Chart.js 集成 Chart.js 是…