- Dapatkan pautan
- X
- E-mel
- Apl Lain
Demo
HTML view ▼
<div class="labu">
<div class="clock" role="img">
<div class="clock__tick clock__tick--0"></div>
<div class="clock__tick clock__tick--3"></div>
<div class="clock__tick clock__tick--6"></div>
<div class="clock__tick clock__tick--9"></div>
<div class="clock__hand clock__hand--hr" data-hand="h"></div>
<div class="clock__hand clock__hand--min" data-hand="m"></div>
<div class="clock__hand clock__hand--sec" data-hand="s"></div>
</div>
</div>
<style>
.labu * {
border: 0;
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--hue: 223;
--bg: hsl(var(--hue),10%,90%);
--fg: hsl(var(--hue),10%,10%);
--primary: hsl(var(--hue),90%,50%);
--shadow1: hsl(var(--hue),10%,80%);
--shadow2: hsl(var(--hue),10%,100%);
--trans-dur: 0.3s;
font-size: calc(16px + (24 - 16) * (100vw - 320px) / (2560 - 320));
}
.labu {
background-color: var(--bg);
color: var(--fg);
display: flex;
font: 1em/1.5 sans-serif;
height: 60vh;
}
.clock,
.clock:before,
.clock__tick:before {
animation-duration: 0.6s;
animation-timing-function: ease-in-out;
animation-fill-mode: forwards;
}
.clock {
animation-name: clockOuter;
background-color: var(--bg);
border-radius: 50%;
box-shadow:
0 0 0 var(--shadow1),
0 0 0 var(--shadow2);
margin: auto;
position: relative;
width: 16em;
height: 16em;
}
.clock:before,
.clock__hand,
.clock__hand:before,
.clock__tick,
.clock__tick:before {
position: absolute;
}
.clock:before,
.clock__hand:before,
.clock__tick:before {
content: "";
display: block;
}
.clock:before {
animation-name: clockInner;
border-radius: inherit;
box-shadow:
0 0 0 var(--shadow1),
0 0 0 var(--shadow2);
top: calc(50% - 4em);
left: calc(50% - 4em);
width: 8em;
height: 8em;
z-index: 1;
}
.clock__hand,
.clock__hand:before {
transform-origin: 50% calc(100% - 1em);
}
.clock__hand {
bottom: calc(50% - 1em);
left: calc(50% - 0.125em);
width: 0.25em;
transform: scaleY(0);
z-index: 2;
}
.clock__hand:before {
width: 100%;
height: 100%;
}
.clock__hand--hr {
height: 4em;
}
.clock__hand--hr:before {
background-color: hsl(var(--hue),10%,50%);
}
.clock__hand--min {
height: 6em;
}
.clock__hand--min:before {
background-color: var(--fg);
}
.clock__hand--sec {
height: 7em;
}
.clock__hand--sec:before {
background-color: var(--primary);
}
.clock--moving .clock__hand--sec:before {
animation: clockSecondHand 0.5s ease-in-out;
}
.clock__tick {
bottom: 50%;
left: calc(50% - 0.125em);
width: 0.25em;
height: 1.25em;
transform-origin: 50% 100%;
}
.clock__tick:before {
background-color: hsla(var(--hue),10%,50%,0.5);
animation-name: clockTick;
width: 100%;
height: 100%;
transform-origin: 50% 0%;
}
.clock__tick--0 {
transform: translateY(-6.5em);
}
.clock__tick--3 {
transform: rotate(90deg) translateY(-6.5em);
}
.clock__tick--6 {
transform: rotate(180deg) translateY(-6.5em);
}
.clock__tick--9 {
transform: rotate(270deg) translateY(-6.5em);
}
@media (prefers-color-scheme: dark) {
:root {
--bg: hsl(var(--hue),10%,20%);
--fg: hsl(var(--hue),10%,90%);
--shadow1: hsl(var(--hue),10%,10%);
--shadow2: hsl(var(--hue),10%,30%);
}
}
@keyframes clockInner {
from,
50% {
box-shadow:
0 0 0 var(--shadow1),
0 0 0 var(--shadow2);
}
75%,
to {
box-shadow:
0.375em 0.375em 0.75em var(--shadow1),
-0.375em -0.375em 0.75em var(--shadow2);
}
}
@keyframes clockOuter {
from,
25% {
box-shadow:
0 0 0 var(--shadow1),
0 0 0 var(--shadow2);
}
50%,
to {
box-shadow:
0.75em 0.75em 1.5em var(--shadow1),
-0.75em -0.75em 1.5em var(--shadow2);
}
}
@keyframes clockSecondHand {
from,
25% {
transform: rotate(-6deg);
}
50% {
transform: rotate(2deg);
}
75% {
transform: rotate(-1deg);
}
to {
transform: rotate(0deg);
}
}
@keyframes clockTick {
from,
75% {
transform: scaleY(0);
}
to {
transform: scaleY(1);
}
}
</style>
<script>
window.addEventListener("DOMContentLoaded",() => {
const c = new Clock1(".clock");
});
class Clock1 {
timeUpdateLoop = null;
constructor(el) {
this.el = document.querySelector(el);
this.init();
}
init() {
this.timeUpdate(true);
}
get timeAsObject() {
const date = new Date();
let h = date.getHours();
const m = date.getMinutes();
const s = date.getSeconds();
const ap = h > 11 ? "PM" : "AM";
const since1970 = date.getTime() - date.getTimezoneOffset() * 60 * 1000;
if (h === 0) h += 12;
else if (h > 12) h -= 12;
return { h, m, s, ap, since1970 };
}
get timeAsString() {
let { h, m, s, ap } = this.timeAsObject;
if (m < 10) m = `0${m}`;
if (s < 10) s = `0${s}`;
return `${h}:${m}:${s} ${ap}`;
}
timeUpdate(firstTime) {
if (!firstTime) {
const movingClass = "clock--moving";
this.el?.classList.remove(movingClass);
void this.el.offsetWidth;
this.el?.classList.add(movingClass);
}
this.el?.setAttribute("aria-label", this.timeAsString);
const time = this.timeAsObject;
Object.keys(time).forEach(hand => {
const handEl = this.el?.querySelector(`[data-hand="${hand}"]`);
if (handEl) {
let angle;
let animateOffset;
const roundDownSec = Math.floor(time.since1970 / 1000) * 1000;
switch (hand) {
case "h":
const msIn12Hrs = 60 * 60 * 12 * 1000;
angle = roundDownSec % msIn12Hrs / msIn12Hrs;
animateOffset = 0.6;
break;
case "m":
const msIn1Hr = 60 * 60 * 1000;
angle = roundDownSec % msIn1Hr / msIn1Hr;
animateOffset = 0.75;
break;
default:
const msIn1Min = 60 * 1000;
angle = roundDownSec % msIn1Min / msIn1Min;
animateOffset = 0.9;
}
angle *= 360;
if (firstTime === true) {
handEl.animate(
[
{ offset: 0, transform: "rotate(0deg) scaleY(0)" },
{ offset: 0.3, transform: "rotate(0deg) scaleY(0)" },
{ offset: animateOffset, transform: `rotate(${angle}deg) scaleY(1)` },
{ offset: 1, transform: `rotate(${angle}deg) scaleY(1)` },
],
{
duration: 1000,
easing: "ease-in-out"
}
);
}
handEl.style.transform = `rotate(${angle}deg)`;
}
});
clearTimeout(this.timeUpdateLoop);
this.timeUpdateLoop = setTimeout(this.timeUpdate.bind(this),1e3);
}
}
</script>
<div class="clock" role="img">
<div class="clock__tick clock__tick--0"></div>
<div class="clock__tick clock__tick--3"></div>
<div class="clock__tick clock__tick--6"></div>
<div class="clock__tick clock__tick--9"></div>
<div class="clock__hand clock__hand--hr" data-hand="h"></div>
<div class="clock__hand clock__hand--min" data-hand="m"></div>
<div class="clock__hand clock__hand--sec" data-hand="s"></div>
</div>
</div>
<style>
.labu * {
border: 0;
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--hue: 223;
--bg: hsl(var(--hue),10%,90%);
--fg: hsl(var(--hue),10%,10%);
--primary: hsl(var(--hue),90%,50%);
--shadow1: hsl(var(--hue),10%,80%);
--shadow2: hsl(var(--hue),10%,100%);
--trans-dur: 0.3s;
font-size: calc(16px + (24 - 16) * (100vw - 320px) / (2560 - 320));
}
.labu {
background-color: var(--bg);
color: var(--fg);
display: flex;
font: 1em/1.5 sans-serif;
height: 60vh;
}
.clock,
.clock:before,
.clock__tick:before {
animation-duration: 0.6s;
animation-timing-function: ease-in-out;
animation-fill-mode: forwards;
}
.clock {
animation-name: clockOuter;
background-color: var(--bg);
border-radius: 50%;
box-shadow:
0 0 0 var(--shadow1),
0 0 0 var(--shadow2);
margin: auto;
position: relative;
width: 16em;
height: 16em;
}
.clock:before,
.clock__hand,
.clock__hand:before,
.clock__tick,
.clock__tick:before {
position: absolute;
}
.clock:before,
.clock__hand:before,
.clock__tick:before {
content: "";
display: block;
}
.clock:before {
animation-name: clockInner;
border-radius: inherit;
box-shadow:
0 0 0 var(--shadow1),
0 0 0 var(--shadow2);
top: calc(50% - 4em);
left: calc(50% - 4em);
width: 8em;
height: 8em;
z-index: 1;
}
.clock__hand,
.clock__hand:before {
transform-origin: 50% calc(100% - 1em);
}
.clock__hand {
bottom: calc(50% - 1em);
left: calc(50% - 0.125em);
width: 0.25em;
transform: scaleY(0);
z-index: 2;
}
.clock__hand:before {
width: 100%;
height: 100%;
}
.clock__hand--hr {
height: 4em;
}
.clock__hand--hr:before {
background-color: hsl(var(--hue),10%,50%);
}
.clock__hand--min {
height: 6em;
}
.clock__hand--min:before {
background-color: var(--fg);
}
.clock__hand--sec {
height: 7em;
}
.clock__hand--sec:before {
background-color: var(--primary);
}
.clock--moving .clock__hand--sec:before {
animation: clockSecondHand 0.5s ease-in-out;
}
.clock__tick {
bottom: 50%;
left: calc(50% - 0.125em);
width: 0.25em;
height: 1.25em;
transform-origin: 50% 100%;
}
.clock__tick:before {
background-color: hsla(var(--hue),10%,50%,0.5);
animation-name: clockTick;
width: 100%;
height: 100%;
transform-origin: 50% 0%;
}
.clock__tick--0 {
transform: translateY(-6.5em);
}
.clock__tick--3 {
transform: rotate(90deg) translateY(-6.5em);
}
.clock__tick--6 {
transform: rotate(180deg) translateY(-6.5em);
}
.clock__tick--9 {
transform: rotate(270deg) translateY(-6.5em);
}
@media (prefers-color-scheme: dark) {
:root {
--bg: hsl(var(--hue),10%,20%);
--fg: hsl(var(--hue),10%,90%);
--shadow1: hsl(var(--hue),10%,10%);
--shadow2: hsl(var(--hue),10%,30%);
}
}
@keyframes clockInner {
from,
50% {
box-shadow:
0 0 0 var(--shadow1),
0 0 0 var(--shadow2);
}
75%,
to {
box-shadow:
0.375em 0.375em 0.75em var(--shadow1),
-0.375em -0.375em 0.75em var(--shadow2);
}
}
@keyframes clockOuter {
from,
25% {
box-shadow:
0 0 0 var(--shadow1),
0 0 0 var(--shadow2);
}
50%,
to {
box-shadow:
0.75em 0.75em 1.5em var(--shadow1),
-0.75em -0.75em 1.5em var(--shadow2);
}
}
@keyframes clockSecondHand {
from,
25% {
transform: rotate(-6deg);
}
50% {
transform: rotate(2deg);
}
75% {
transform: rotate(-1deg);
}
to {
transform: rotate(0deg);
}
}
@keyframes clockTick {
from,
75% {
transform: scaleY(0);
}
to {
transform: scaleY(1);
}
}
</style>
<script>
window.addEventListener("DOMContentLoaded",() => {
const c = new Clock1(".clock");
});
class Clock1 {
timeUpdateLoop = null;
constructor(el) {
this.el = document.querySelector(el);
this.init();
}
init() {
this.timeUpdate(true);
}
get timeAsObject() {
const date = new Date();
let h = date.getHours();
const m = date.getMinutes();
const s = date.getSeconds();
const ap = h > 11 ? "PM" : "AM";
const since1970 = date.getTime() - date.getTimezoneOffset() * 60 * 1000;
if (h === 0) h += 12;
else if (h > 12) h -= 12;
return { h, m, s, ap, since1970 };
}
get timeAsString() {
let { h, m, s, ap } = this.timeAsObject;
if (m < 10) m = `0${m}`;
if (s < 10) s = `0${s}`;
return `${h}:${m}:${s} ${ap}`;
}
timeUpdate(firstTime) {
if (!firstTime) {
const movingClass = "clock--moving";
this.el?.classList.remove(movingClass);
void this.el.offsetWidth;
this.el?.classList.add(movingClass);
}
this.el?.setAttribute("aria-label", this.timeAsString);
const time = this.timeAsObject;
Object.keys(time).forEach(hand => {
const handEl = this.el?.querySelector(`[data-hand="${hand}"]`);
if (handEl) {
let angle;
let animateOffset;
const roundDownSec = Math.floor(time.since1970 / 1000) * 1000;
switch (hand) {
case "h":
const msIn12Hrs = 60 * 60 * 12 * 1000;
angle = roundDownSec % msIn12Hrs / msIn12Hrs;
animateOffset = 0.6;
break;
case "m":
const msIn1Hr = 60 * 60 * 1000;
angle = roundDownSec % msIn1Hr / msIn1Hr;
animateOffset = 0.75;
break;
default:
const msIn1Min = 60 * 1000;
angle = roundDownSec % msIn1Min / msIn1Min;
animateOffset = 0.9;
}
angle *= 360;
if (firstTime === true) {
handEl.animate(
[
{ offset: 0, transform: "rotate(0deg) scaleY(0)" },
{ offset: 0.3, transform: "rotate(0deg) scaleY(0)" },
{ offset: animateOffset, transform: `rotate(${angle}deg) scaleY(1)` },
{ offset: 1, transform: `rotate(${angle}deg) scaleY(1)` },
],
{
duration: 1000,
easing: "ease-in-out"
}
);
}
handEl.style.transform = `rotate(${angle}deg)`;
}
});
clearTimeout(this.timeUpdateLoop);
this.timeUpdateLoop = setTimeout(this.timeUpdate.bind(this),1e3);
}
}
</script>
Rujukan CSS & JS di codepen.io/jkantner/pen/oNJMQWj
Ulasan