写一个年月日时分秒毫秒html电脑桌面转动的时钟
<!DOCTYPE html>
<html>
<head>
<title>电脑桌面时钟</title>
<style>
.clock {
font-size: 3em;
font-weight: bold;
text-align: center;
margin-top: 50px;
transform-origin: center center;
animation: rotate 1s linear infinite;
}
<pre><code> @keyframes rotate {
from {
transform: rotateZ(0deg);
}
to {
transform: rotateZ(360deg);
}
}
</style>
</code></pre>
</head>
<body>
<div class="clock" id="clock"></div>
<pre><code><script>
function updateClock() {
let now = new Date();
let year = now.getFullYear();
let month = now.getMonth() + 1;
if (month < 10) {
month = "0" + month;
}
let day = now.getDate();
if (day < 10) {
day = "0" + day;
}
let hours = now.getHours();
if (hours < 10) {
hours = "0" + hours;
}
let minutes = now.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
}
let seconds = now.getSeconds();
if (seconds < 10) {
seconds = "0" + seconds;
}
let milliseconds = now.getMilliseconds();
if (milliseconds < 10) {
milliseconds = "00" + milliseconds;
} else if (milliseconds < 100) {
milliseconds = "0" + milliseconds;
}
let clock = document.getElementById("clock");
clock.innerHTML = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}
setInterval(updateClock, 1);
</script>
</code></pre>
</body>
</html
原文地址: https://www.cveoy.top/t/topic/fnFf 著作权归作者所有。请勿转载和采集!