WEBLEB
Home
Editor
Login
Pro
English
English
Français
Español
Português
Deutsch
Italiano
हिंदी
HTML
CSS
JS
body { background: #2b55dd; } #clock { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: calc(100vw - 4px); height: auto; max-width: 300px; }
function initClock(elt, opt_properties) { const defaultProperties = { color: "#0ff", date: new Date(), glow: true, militaryTime: false, showMs: true }; const c = Object.assign(defaultProperties, opt_properties); !c.dotColor ? (c.dotColor = defaultProperties.color) : false; const getSecondsPassed = (now = c.date, militaryTime = c.militaryTime) => { const s10 = Math.floor(now.getSeconds()); const m10 = Math.floor(now.getMinutes() * 60 + s10); const h = Math.floor(now.getHours() * 3600 + m10); return { hour: h, minute10: m10, minute1: Math.floor((now.getMinutes() % 10) * 60 + s10), second10: s10, second1: Math.floor(now.getSeconds() % 10) }; }; let td = getSecondsPassed(); let vxEnd = c.showMs ? 175 : 130; let showGlow = c.glow ? `
` : ""; let showGlowBlur = c.glow ? `
` : ""; let hrDur = c.militaryTime ? "86400" : "43200"; let hr1Offsets = c.militaryTime ? `0 0; 0 -30; 0 -60; 0 -90; 0 -120; 0 -150; 0 -180; 0 -210; 0 -240; 0 -270; 0 0; 0 -30; 0 -60; 0 -90; 0 -120; 0 -150; 0 -180; 0 -210; 0 -240; 0 -270; 0 0; 0 -30; 0 -60; 0 -90` : `0 -60; 0 -30; 0 -60; 0 -90; 0 -120; 0 -150; 0 -180; 0 -210; 0 -240; 0 -270; 0 0; 0 -30`; let hr10Offsets = c.militaryTime ? `0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -60; 0 -60; 0 -60; 0 -60` : `0 -30; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 -30; 0 -30`; let msDots = c.showMs ? `M 132.5 10 v0 m0 10 v0` : ""; let milliseconds = c.showMs ? `
` : ""; let svg = `
${showGlowBlur}
${milliseconds}
${showGlow}
`; const wrapper = document.createElement("div"); wrapper.innerHTML = svg; const doc = wrapper.firstChild; elt.appendChild(doc); } initClock(document.querySelector("#clock"), { color: "#D5FF77", dotColor: "#80AACC", showMs: true }); // refresh it if we leave the page document.addEventListener("visibilitychange", () => { if (!document.hidden) { clock.innerHTML = ""; initClock(document.querySelector("#clock"), { color: "#D5FF77", dotColor: "#80AACC", showMs: true }); } });
Validating your code, please wait...
HTML
CSS
JS
body { background: #2b55dd; } #clock { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: calc(100vw - 4px); height: auto; max-width: 300px; }
function initClock(elt, opt_properties) { const defaultProperties = { color: "#0ff", date: new Date(), glow: true, militaryTime: false, showMs: true }; const c = Object.assign(defaultProperties, opt_properties); !c.dotColor ? (c.dotColor = defaultProperties.color) : false; const getSecondsPassed = (now = c.date, militaryTime = c.militaryTime) => { const s10 = Math.floor(now.getSeconds()); const m10 = Math.floor(now.getMinutes() * 60 + s10); const h = Math.floor(now.getHours() * 3600 + m10); return { hour: h, minute10: m10, minute1: Math.floor((now.getMinutes() % 10) * 60 + s10), second10: s10, second1: Math.floor(now.getSeconds() % 10) }; }; let td = getSecondsPassed(); let vxEnd = c.showMs ? 175 : 130; let showGlow = c.glow ? `
` : ""; let showGlowBlur = c.glow ? `
` : ""; let hrDur = c.militaryTime ? "86400" : "43200"; let hr1Offsets = c.militaryTime ? `0 0; 0 -30; 0 -60; 0 -90; 0 -120; 0 -150; 0 -180; 0 -210; 0 -240; 0 -270; 0 0; 0 -30; 0 -60; 0 -90; 0 -120; 0 -150; 0 -180; 0 -210; 0 -240; 0 -270; 0 0; 0 -30; 0 -60; 0 -90` : `0 -60; 0 -30; 0 -60; 0 -90; 0 -120; 0 -150; 0 -180; 0 -210; 0 -240; 0 -270; 0 0; 0 -30`; let hr10Offsets = c.militaryTime ? `0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -30; 0 -60; 0 -60; 0 -60; 0 -60` : `0 -30; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 0; 0 -30; 0 -30`; let msDots = c.showMs ? `M 132.5 10 v0 m0 10 v0` : ""; let milliseconds = c.showMs ? `
` : ""; let svg = `
${showGlowBlur}
${milliseconds}
${showGlow}
`; const wrapper = document.createElement("div"); wrapper.innerHTML = svg; const doc = wrapper.firstChild; elt.appendChild(doc); } initClock(document.querySelector("#clock"), { color: "#D5FF77", dotColor: "#80AACC", showMs: true }); // refresh it if we leave the page document.addEventListener("visibilitychange", () => { if (!document.hidden) { clock.innerHTML = ""; initClock(document.querySelector("#clock"), { color: "#D5FF77", dotColor: "#80AACC", showMs: true }); } });