本文实例为大家分享了JavaScript实现限时秒杀功能的具体代码,供大家参考,具体内容如下
文件index.html
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!DOCTYPE html> < html > < head > < meta charset = "GBK" /> < title >show</ title > < link rel = "stylesheet" href = "css/index.css" type = "text/css" /> </ head > < body > < div class = "divMain" > < img src = "img/secondBuyImg.jpg" id = "imgMain" /> < h3 style = "color: #FF0000;" >距离本厂秒杀结束还剩:</ h3 > < div id = "divSecond" > < div id = "divF1" class = "divFours" > < font class = "fontdate" size = "4" id = "font1" >00</ font > < font class = "fonttext" size = "4" >天</ font > </ div > < div id = "divF2" class = "divFours" > < font class = "fontdate" size = "4" id = "font2" >00</ font > < font class = "fonttext" size = "4" >时</ font > </ div > < div id = "divF3" class = "divFours" > < font class = "fontdate" size = "4" id = "font3" >00</ font > < font class = "fonttext" size = "4" >分</ font > </ div > < div id = "divF4" class = "divFours" > < font class = "fontdate" size = "4" id = "font4" >00</ font > < font class = "fonttext" size = "4" >秒</ font > </ div > </ div > </ div > </ body > </ html > < script type = "text/javascript" src = "js/index.js" ></ script > |
样式表文件index.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #imgMain{ } .divMain{ /*外层div*/ width : 100% ; height : 100% ; display : flex; justify- content : left ; align-items: center ; /*位于弹性盒子垂直方向的中心*/ flex- direction : column; /*设置弹性盒子排序方向*/ } /* 外层时间div */ #divSecond{ width : 500px ; height : 500px ; display : flex; justify- content : center ; align-items: flex-start; flex- direction : row; } .divFours{ /*时间div1-4*/ width : 10% ; height : 10% ; border : 2px solid red ; display : flex; flex- direction : row; justify- content : center ; align-items: center ; } /* 图片外层div */ #divFrist{ width : 20% ; } /* 剩余时间显示 */ .fontdate{ color : deeppink; } .fonttext{ color : darkblue; } |
JavaScript文件index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | function torun(str){ var date= new Date(); var systemDay=date.getDate(); var systemHour=date.getHours(); var systemMinute=date.getMinutes(); var systemSecond=date.getSeconds(); var endDate= new Date(str); var endDay=endDate.getDate(); var endHour=endDate.getHours(); var endMinute=endDate.getMinutes(); var endSecond=endDate.getSeconds(); var remainingDay=-(systemDay-endDay)-1; var remainingHour=-(systemHour-endHour)-1; var remainingMinute=-(systemMinute-endMinute)-1; var remainingSecond=(systemSecond-endSecond>=0)?systemSecond-endSecond+59:-(systemSecond-endSecond)-1; console.log( "剩余" +remainingDay+ "天" ); console.log( "剩余" +remainingHour+ "时" ); console.log( "剩余" +remainingMinute+ "分" ); console.log( "剩余" +remainingSecond+ "秒" ); console.log( "当前天" +systemDay+ "结束天" +endDay); console.log( "当前时" +systemHour+ "结束时" +endHour); console.log( "当前分" +systemMinute+ "结束分" +endMinute); console.log( "当前秒" +systemSecond+ "结束秒" +endSecond); document.getElementById( "font1" ).innerText=remainingDay+ "" ; document.getElementById( "font2" ).innerText=remainingHour+ "" ; document.getElementById( "font3" ).innerText=remainingMinute+ "" ; document.getElementById( "font4" ).innerText=remainingSecond+ "" ; } var int =setInterval( 'torun("2020-12-5 23:59:59")' ,60); |
目录
效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/220799/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)