Use native JavaScript to simply implement the countdown, for your reference, the specific content is as follows Effect Code // An highlighted block <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <!-- css style --> <style type="text/css"> * { margin: 0; padding: 0; } .onsell { height: 400px; width: 200px; border: 1px solid #F2F2F2; margin: 10px; box-shadow: 1px 2px 4px rgba(0, 0, 0, .2); } .box { /* display: none; */ float: left; margin: 328px 34px 0; } .box div { position: relative; display: inline-block; width: 40px; height: 40px; background-color: #333; color: #fff; font-size: 20px; text-align: center; line-height: 40px; box-shadow: 1px 2px 4px rgba(0, 0, 0, .4); } </style> </head> <body> <!-- Requirement: A product will be on sale for one day in the future, and a countdown effect will be created using js: hours: minutes: seconds --> <div class="onsell"> <div class="box"> <div class="hour">00</div> <div class="minutes">00</div> <div class="seconds">00</div> </div> </div> </body> </html> <script> window.onload = function () { let hour = document.querySelector('.hour') let minutes = document.querySelector('.minutes') let seconds = document.querySelector('.seconds') // The countdown timestamp uses + to convert the time object into a timestamp, which is equivalent to Date.now() let wantTime = +new Date('2021-3-17 18:00:00') countTime() let timer = setInterval(() => { countTime() }, 1000) function countTime() { let currentTime = +new Date() if (wantTime >= currentTime) { let times = (wantTime - currentTime) / 1000 // Total seconds timestamp / 1000 = seconds let remainDay = parseInt(times / 60 / 60 / 24) // The remainder is the remaining days console.log(remainDay); if (remainDay === 0) { if(times < 1) { // Countdown is over // Trigger operation here} // If the number of days is less than one day, start timing setTime(times) } } else { hour.innerHTML = '00' minutes.innerHTML = '00' seconds.innerHTML = '00' } } function setTime(time) { // Rough version let s = parseInt(time % 60) s = s < 10 ? '0' + s : s let m = parseInt(time / 60 % 60) m = m < 10 ? '0' + m : m let h = parseInt(time / 60 / 60 % 24) h = h < 10 ? '0' + h : h hour.innerHTML = h minutes.innerHTML = m seconds.innerHTML = seconds } } </script> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of software configuration using docker-compose in linux
>>: Detailed explanation of replace into example in mysql
#docker search #docker pull portainer 1. Download...
What is element-ui element-ui is a desktop compon...
IDEA is the most commonly used development tool f...
About JS, CSS CSS: Stylesheet at the top Avoid CS...
This article shares the specific steps of VMware ...
I have always used Loadrunner to do performance t...
Table of contents One-way data flow explanation V...
Preface When using the MySQL database, sometimes ...
Classical color combinations convey power and auth...
After starting Docker, let's take a look at t...
Often, after a web design is completed, the desig...
This article example shares the specific code of ...
Table of contents Preface The role of render Rend...
1. Basic Use It can be instantiated through the M...
This article shares the specific code of js to re...