forked from JiangZhaojin/Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d3ca17
commit ff03d33
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## 一个简单的倒计时程序 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.container { | ||
width: 100%; | ||
height: 100%; | ||
|
||
padding-top: 100px; | ||
text-align: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>倒计时小程序</title> | ||
<link rel="stylesheet" href="css/main.css"> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1>倒计时</h1> | ||
<div>请输入一个未来的时间:(格式YYYY/MM/DD)</div> | ||
<input type="text" value="2017/06/30"> | ||
<button>倒计时</button> | ||
<div id="show">距离YYYY年MM月DD日还有XX天XX小时XX分XX秒</div> | ||
</div> | ||
<script src="js/main.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var clock; | ||
window.onload = function(){ | ||
var button = document.getElementsByTagName("button")[0]; | ||
// 不管ie啦 | ||
button.addEventListener("click", function(e){ | ||
clearInterval(clock); | ||
var pattern = /^\d{4}\/((0[1-9])|(1[0,1,2]))\/((0[1-9])|([1-2][0-9])|(3[0,1]))$/; | ||
var targetStr = document.getElementsByTagName("input")[0].value; | ||
var targetTime = new Date(targetStr); | ||
var showEle = document.getElementById("show"); | ||
clock = setInterval(function(){ count();},500); | ||
function count(){ | ||
if (targetStr.match(pattern)) { | ||
var nowTime = new Date(); | ||
var gap = targetTime - nowTime; | ||
if (gap < 0) { | ||
showEle.innerHTML = "请输入未来的时间"; | ||
clearInterval(clock); | ||
return; | ||
} else if (gap == 0) { | ||
showEle.innerHTML = "到达时间啦!"; | ||
clearInterval(clock); | ||
return; | ||
} else { | ||
var day = Math.floor(gap/(1000*60*60*24)); | ||
var hour = Math.floor(gap/(1000*60*60)%24); | ||
var minute = Math.floor(gap/(1000*60)%60); | ||
var second = Math.floor(gap/1000%60); | ||
showEle.innerHTML = "距离"+targetStr+"还有"+day+"天"+hour+"小时"+minute+"分"+second+"秒"; | ||
} | ||
} else { | ||
showEle.innerHTML = "请输入正确格式的时间"; | ||
} | ||
} | ||
|
||
}); | ||
} |