Home Web Front-end JS Tutorial Implement beautiful and practical countdown example code based on jQuery_jquery

Implement beautiful and practical countdown example code based on jQuery_jquery

May 16, 2016 pm 03:22 PM

Countdown effects have a wide range of applications, such as Olympic Games countdown, college entrance examination countdown and holiday countdown, etc. This section shares a more beautiful and practical countdown effect.

The code example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>倒计时效果代码</title>
<style type="text/css">
* {
 padding:0;
 margin:0;
}
.colockbox {
 width:250px;
 height:30px;
 overflow:hidden;
 color:#000000;
 background:url(mytest/jQuery/colockbg.png) no-repeat;
 margin:0px auto;
}
.colockbox span {
 float:left;
 display:block;
 width:40px;
 height:29px;
 line-height:29px;
 font-size:20px;
 font-weight:bold;
 text-align:center;
 color:#ffffff;
 margin-right:22px;
}
</style>
<script type="text/javascript" src="http://www.softwhy.com/mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(function(){ 
 countDown("2016/2/3 6:30:59","#colockbox1"); 
}); 
function countDown(time,id){ 
 var day_elem=$(id).find('.day'); 
 var hour_elem=$(id).find('.hour'); 
 var minute_elem=$(id).find('.minute'); 
 var second_elem=$(id).find('.second'); 
 var end_time = new Date(time).getTime();
 var sys_second = (end_time-new Date().getTime())/1000; 
 var timer = setInterval(function(){ 
  if(sys_second>1) { 
   sys_second-=1; 
   var day=Math.floor((sys_second/3600)/24); 
   var hour=Math.floor((sys_second/3600)%24); 
   var minute=Math.floor((sys_second/60)%60); 
   var second=Math.floor(sys_second%60); 
   $(day_elem).text(day);
   $(hour_elem).text(hour<10&#63;"0"+hour:hour);
   $(minute_elem).text(minute<10&#63;"0"+minute:minute); 
   $(second_elem).text(second<10&#63;"0"+second:second);
  } 
  else { 
   clearInterval(timer); 
  } 
 }, 1000); 
} 
</script>
</head>
<body>
<div class="colockbox" id="colockbox1"> 
 <span class="day">00</span> 
 <span class="hour">00</span> 
 <span class="minute">00</span> 
 <span class="second">00</span> 
</div>
</body>
</html> 
Copy after login

The above code meets our requirements and can achieve a countdown effect from seconds to days. The implementation process is introduced below.

1. Implementation principle:

The principle is relatively simple. It is to get the timestamp of the expiration time minus the timestamp of the current time, which is the number of seconds difference between the two. Then divide this number of seconds by 3600 to get the number of hours difference, and then divide Take 24, and then use the Math.floor() function to round down, which is the number of days. This principle is used to obtain hours, minutes, and seconds below. Use the timer function to call the corresponding function every second to achieve the countdown effect.

2. Code comments:

1.$(function(){}), when the document structure is completely loaded, execute the code in the function.
2.countDown("2016/2/3 6:30:59","#colockbox1"), call the function, the first parameter is the expiration time, and the second parameter is the id attribute value of the div.
3.function countDown(time,id){}, declare this function.
4.var day_elem=$(id).find('.day'), get the object whose class attribute value is day under the div.
5.var hour_elem=$(id).find('.hour'), ​​get the object whose class attribute value is hour under the div.
6.var minute_elem=$(id).find('.minute'), get the object whose class attribute value is minute under the div.
7.var second_elem=$(id).find('.second'), get the object whose class attribute value is second under the div.
8.var end_time=new Date(time).getTime(), get the timestamp of the expiration event.
9.var sys_second=(end_time-new Date().getTime())/1000, get the number of seconds difference between the expiration time and the current time.
10.var timer=setInterval(function(){}, 1000), execute the function every second.
11.if(sys_second>1) , if the difference in seconds is greater than 1.
12.sys_second-=1, subtract one from the second.
13.var day=Math.floor((sys_second/3600)/24), get the number of days difference.
14.var hour=Math.floor((sys_second/3600)%24), get the number of hours difference, please note that the following is a modulo operation.
15.var minute=Math.floor((sys_second/60)%60), get the number of minutes difference.
16.var second=Math.floor(sys_second%60), get the number of seconds difference.
17.$(day_elem).text(day), writes the day into the span element.
18.$(hour_elem).text(hour<10?"0"+hour:hour), write the hour into span. If the number of hours is less than 10, add 0 in front, and the same reason behind.
19.clearInterval(timer), if the difference in seconds reaches 0, stop the execution of the timer function setInterval.

The above content is a beautiful and practical countdown example code based on jQuery that the editor shared with you. I hope that sharing this article can be helpful to everyone.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig

See all articles