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

WBOY
WBOYOriginal
2016-05-16 15:22:411136browse

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> 

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:
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