mirror of
https://github.com/sigmasternchen/glitch-countdown
synced 2025-03-15 10:38:53 +00:00
feat: Add text input
This commit is contained in:
parent
f1a6deea01
commit
2102b40a88
3 changed files with 116 additions and 8 deletions
38
dist/index.html
vendored
38
dist/index.html
vendored
|
@ -1,9 +1,33 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="main.js"></script>"
|
||||
</head>
|
||||
<body>
|
||||
Hello World
|
||||
</body>
|
||||
</html>
|
||||
|
||||
<head>
|
||||
<title>Glitch Countdown</title>
|
||||
<script src="main.js"></script>
|
||||
<link rel="stylesheet" href="main.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<h1>Glitch Countdown</h1>
|
||||
</header>
|
||||
<nav></nav>
|
||||
<section>
|
||||
<div id="countdown">
|
||||
<div id="days" class="part">
|
||||
<input type="text" value="0">
|
||||
</div>
|
||||
<div id="hours" class="part">
|
||||
<input type="text" value="0"> :
|
||||
</div>
|
||||
<div id="minutes" class="part">
|
||||
<input type="text" value="0"> :
|
||||
</div>
|
||||
<div id="seconds" class="part">
|
||||
<input type="text" value="0">
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</body>
|
||||
|
||||
</html>
|
25
dist/main.css
vendored
Normal file
25
dist/main.css
vendored
Normal file
|
@ -0,0 +1,25 @@
|
|||
body {
|
||||
background-color: black;
|
||||
}
|
||||
|
||||
#countdown {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
width: 50vw;
|
||||
height: 8vw;
|
||||
background-color: grey;
|
||||
}
|
||||
|
||||
#countdown div {
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
#countdown * {
|
||||
font-size: 3vw;
|
||||
}
|
||||
|
||||
#countdown input {
|
||||
width: 6vw;
|
||||
}
|
61
src/index.js
61
src/index.js
|
@ -1 +1,60 @@
|
|||
console.log("Hello World");
|
||||
(function() {
|
||||
const parts = {
|
||||
"days": {
|
||||
"next": null,
|
||||
"prev": "hours",
|
||||
"maxValue": 999,
|
||||
"element": null,
|
||||
"input": null,
|
||||
},
|
||||
"hours": {
|
||||
"next": "days",
|
||||
"prev": "minutes",
|
||||
"maxValue": 24,
|
||||
"element": null,
|
||||
"input": null,
|
||||
},
|
||||
"minutes": {
|
||||
"next": "hours",
|
||||
"prev": "seconds",
|
||||
"maxValue": 60,
|
||||
"element": null,
|
||||
"input": null,
|
||||
},
|
||||
"seconds": {
|
||||
"next": "minutes",
|
||||
"prev": null,
|
||||
"maxValue": 60,
|
||||
"element": null,
|
||||
"input": null,
|
||||
},
|
||||
}
|
||||
|
||||
window.addEventListener("load", function() {
|
||||
Array.from(document.getElementById("countdown").getElementsByClassName("part")).forEach(element => {
|
||||
const part = parts[element.id];
|
||||
part.element = element;
|
||||
part.input = element.getElementsByTagName("input")[0];
|
||||
|
||||
part.input.addEventListener("input", function(event) {
|
||||
part.input.value = part.input.value.replace(/[^0-9]/, "");
|
||||
});
|
||||
part.input.addEventListener("blur", function(event) {
|
||||
const value = parseInt(part.input.value);
|
||||
if (value >= part.maxValue) {
|
||||
if (part.next) {
|
||||
part.input.value = value % part.maxValue;
|
||||
parts[part.next].input.value = Math.floor(value / part.maxValue);
|
||||
parts[part.next].input.dispatchEvent(new FocusEvent("blur"));
|
||||
} else {
|
||||
part.input.value = part.maxValue;
|
||||
}
|
||||
} else if (value < 0) {
|
||||
part.input.value = 0;
|
||||
} else {
|
||||
part.input.value = value;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
})();
|
Loading…
Reference in a new issue