基于jQuery的倒计时实现代码

关于“基于jQuery的倒计时实现代码”的攻略,我们可以分为以下几个步骤详细讲解:

1. 创建html结构

首先,我们需要在html中创建倒计时所需的html结构。

<div id="countdown">
  <span class="days"></span>
  <span class="hours"></span>
  <span class="minutes"></span>
  <span class="seconds"></span>
</div>

2. 导入jQuery

我们需要在html中导入jQuery库,用于实现倒计时的逻辑。

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

3. 编写jQuery代码

在页面加载完成后,我们使用jQuery选择器选中倒计时所需的元素,并通过计算时间差来实现倒计时。

$(document).ready(function(){
  // 目标时间
  var endTime = new Date('2022-01-01 00:00:00').getTime();

  // 每秒更新倒计时时间
  setInterval(function(){
    // 当前时间
    var now = new Date().getTime();

    // 时间差
    var distance = endTime - now;

    // 计算剩余时间
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // 更新倒计时显示
    $('#countdown .days').text(days + '天');
    $('#countdown .hours').text(hours + '小时');
    $('#countdown .minutes').text(minutes + '分钟');
    $('#countdown .seconds').text(seconds + '秒');
  }, 1000);
});

此时,我们的倒计时就已经完成了。

示例说明

示例一

我们可以根据实际需求修改目标时间,比如设置倒计时为距离2022年1月1日还有一天一小时。

// 目标时间
var endTime = new Date('2022-01-01 00:00:00').getTime() - (24 * 60 * 60 * 1000) - (60 * 60 * 1000);

示例二

我们也可以自定义倒计时的样式,比如采用圆形倒计时样式。

<div id="circle-countdown">
  <div class="countdown-item days">
    <span class="countdown-value"></span>
    <span class="countdown-label">天</span>
  </div>
  <div class="countdown-item hours">
    <span class="countdown-value"></span>
    <span class="countdown-label">小时</span>
  </div>
  <div class="countdown-item minutes">
    <span class="countdown-value"></span>
    <span class="countdown-label">分钟</span>
  </div>
  <div class="countdown-item seconds">
    <span class="countdown-value"></span>
    <span class="countdown-label">秒</span>
  </div>
</div>
#circle-countdown {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
}

.countdown-item {
  position: relative;
  width: 50px;
  height: 50px;
  margin: 0 10px;
  border-radius: 50%;
  color: #fff;
  background-color: #999;
  text-align: center;
  font-size: 20px;
  line-height: 50px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  z-index: 1;
}

.countdown-item:before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #f00;
  z-index: -1;
  transform: scale(1, 1);
  transition: transform 1s cubic-bezier(0.5, 1.6, 0.4, 0.5), box-shadow 1s;
}

.countdown-item.days:before {
  background-color: #0ff;
}

.countdown-item.hours:before {
  background-color: #f0f;
}

.countdown-item.minutes:before {
  background-color: #ff0;
}

.countdown-item.seconds:before {
  background-color: #0f0;
}

.countdown-item .countdown-value {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.countdown-item .countdown-label {
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  font-weight: bold;
}
$(document).ready(function(){
  // 目标时间
  var endTime = new Date('2022-01-01 00:00:00').getTime();

  // 每秒更新倒计时时间
  setInterval(function(){
    // 当前时间
    var now = new Date().getTime();

    // 时间差
    var distance = endTime - now;

    // 计算剩余时间
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // 更新倒计时显示
    $('#circle-countdown .days .countdown-value').text(days);
    $('#circle-countdown .hours .countdown-value').text(hours);
    $('#circle-countdown .minutes .countdown-value').text(minutes);
    $('#circle-countdown .seconds .countdown-value').text(seconds);

    // 更新圆形倒计时样式
    $('#circle-countdown .countdown-item:before').css('transform', 'scale(' + (10 - seconds) / 10 + ')');
  }, 1000);
});

以上就是基于jQuery实现倒计时的完整攻略,希望能对你有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于jQuery的倒计时实现代码 - Python技术站

(0)
上一篇 2023年5月28日
下一篇 2023年5月28日

相关文章

  • 用jQuery简化JavaScript开发分析

    用 jQuery 简化 JavaScript 开发分析 什么是 jQuery? jQuery 是一个 JavaScript 库,它使编写 JavaScript 更加容易。 jQuery 的概念很简单明了,它允许我们以一种更易于使用的方式处理 HTML 文档、处理事件、创建动画、处理 AJAX 等。 为什么要使用 jQuery? 使用 jQuery 有以下好处…

    jquery 2023年5月28日
    00
  • jQuery学习笔记之 Ajax操作篇(一) – 数据加载

    下面是对于这篇文章的详细讲解。 标题 文章的标题为:“jQuery学习笔记之 Ajax操作篇(一) – 数据加载”,其中包括了文章所属的主题——jQuery 学习笔记,以及本文主要讲述的内容——Ajax 操作,且在后面加上了(一)的标识,表示本文是此主题下的第一篇。 阅读目的 本文的主要目的是帮助读者了解 Ajax 的基本概念和使用方法,并提供两个具体的数据…

    jquery 2023年5月27日
    00
  • jQWidgets jqxChart getColorScheme()方法

    jQWidgets 的 jqxChart 组件提供了 getColorScheme() 方法,用于获取当前图表的颜色方案。本文将详细介绍 getColorScheme() 方法的使用方法,包括概述、示例以及注意事项。 getColorScheme() 方法概述 getColorScheme() 方法用于获取当前图表的颜色方案。该方法返回一个包含颜色方案名称和…

    jquery 2023年5月11日
    00
  • jQWidgets jqxScheduler editDialogOpen事件

    以下是关于 jQWidgets jqxScheduler 组件中 editDialogOpen 事件的详细攻略。 jQWidgets jqxScheduler editDialogOpen 事件 jQWidgets jqxScheduler 组件的 editDialogOpen 用于在编辑对话框打开时触发相应的操作。 方法 // 绑定 editDialogO…

    jquery 2023年5月12日
    00
  • jQWidgets jqxTabs collapse()方法

    针对“jQWidgets jqxTabs collapse()方法”的完整攻略,请参考以下内容: 概述 jqxTabs 是 jQWidgets 框架中提供的一种选项卡组件,通过该组件可以轻松地实现选项卡切换的功能。而 collapse() 方法则是该组件的一个内置方法,其中 collapse() 用于关闭当前选项卡。 方法参数 collapse() 方法的参…

    jquery 2023年5月12日
    00
  • .NET中StringBuilder用法实例分析

    先来简要介绍一下 “.NET中StringBuilder用法” 是什么。 StringBuilder是 .NET Framework提供的一个字符串处理类,它能够高效地添加、删除、修改、替换、插入、追加字符等操作。StringBuilder对象是可变的,并且能够使你更有效地对字符串进行拼接、修改操作。 下面,我将详细讲解“.NET中StringBuilder…

    jquery 2023年5月28日
    00
  • jQuery如何跳转到另一个网页 就这么简单

    当我们需要通过点击页面元素实现页面跳转的时候,我们可以使用jQuery提供的跳转方法。下面是使用jQuery实现页面跳转的一些常见方法。 直接修改页面链接的href属性 通过直接修改页面元素的href属性可以实现页面跳转。如下所示: // 使用jquery获取链接元素并修改href属性值 $(‘a’).attr(‘href’, ‘http://www.exa…

    jquery 2023年5月28日
    00
  • Underscore.js _.max函数

    Underscore.js _.max 函数 Underscore.js 是一个 JavaScript 工具库,它提供了诸多实用的函数以方便我们处理数据。 其中,_.max 函数可以用来从一个集合中找到最大的元素。 下面,我们详细讲解一下 _.max 函数的使用方法和参数。 语法 _.max(list, [iteratee], [context]) 参数 l…

    jquery 2023年5月12日
    00
合作推广
合作推广
分享本页
返回顶部