


Implement beautiful and practical countdown example code based on jQuery_jquery
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?"0"+hour:hour); $(minute_elem).text(minute<10?"0"+minute:minute); $(second_elem).text(second<10?"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>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
