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
1. The significance of building nexus service As ...
Function currying (black question mark face)? ? ?...
Table of contents 1. Build local storage 2. Creat...
Suppose there is a table: reward (reward table), ...
In the previous article, I introduced how to solv...
When optimizing a website, we must learn to use e...
Table of contents Structural inheritance (impleme...
Today is still a case of Watch app design. I love...
In addition to B-Tree indexes, MySQL also provide...
Problem: The null type data returned by mybatis d...
Preface Mobile devices have higher requirements f...
The database installation tutorial of MySQL-8.0.2...
I saw this question in the SQL training question ...
Preface: This article mainly introduces the query...
Table of contents Web Development 1. Overview of ...