Back
Innovate your forms with a time input slider masquerading as a retro radio tuner, offering playful exploration of time selection with delightful feedback.
Time is a fundamental component in many applications, especially for scheduling tasks, setting alarms, or marking events. However, the traditional time input methods often lack flair and engagement. What if selecting time was as enjoyable as tuning into your favorite radio station?
Inspired by the classic radio tuner, this time input slider masquerades as a retro tuner, offering playful exploration of time selection with delightful feedback. Let’s delve into how we can build this unique UI component using a perfect blend of HTML, CSS, and JavaScript.
We begin by setting up a simple HTML structure to encompass our time tuner. We use a div with the class .time-tuner-container to centralize our input range and display. Here’s the markup:
<div class="time-tuner-container">
<label for="time-tuner" class="time-tuner-label">Select Time</label>
<input type="range" id="time-tuner" class="time-tuner" min="0" max="1440" step="1" oninput="updateTunerDisplay()" aria-label="Time tuner" />
<div class="tuner-display">
<span class="current-time">12:00 AM</span>
</div>
</div>
This setup includes a range input which spans 0 to 1440 minutes, corresponding to 24 hours. The oninput event triggers the JavaScript function to update the displayed time.
Now, let us breathe life into the component with JavaScript. The function updateTunerDisplay() converts the sliders' minute value to a readable time format.
function updateTunerDisplay() {
const tuner = document.getElementById('time-tuner');
const display = document.querySelector('.current-time');
const minutes = parseInt(tuner.value);
const hours = Math.floor(minutes / 60);
const mins = minutes % 60;
const period = hours >= 12 ? 'PM' : 'AM';
const adjustedHour = (hours % 12) || 12;
const formattedTime = adjustedHour + ':' + (mins < 10 ? '0' : '') + mins + ' ' + period;
display.textContent = formattedTime;
}
This function responds dynamically to user adjustments, recalculating and updating the time shown.
Our CSS leverages both simplicity and flair. The slider's range is visually represented with a gradient, while the thumb radiates a retro feel with a white circular design. Glassmorphism is employed for the display, offering a sense of depth and intrigue.
.time-tuner-container {
display: flex;
flex-direction: column;
align-items: center;
font-family: 'Courier New', Courier, monospace;
width: 100%;
max-width: 500px;
margin: auto;
}
.time-tuner-label {
font-size: 20px;
margin-bottom: 10px;
}
.time-tuner {
-webkit-appearance: none;
width: 100%;
background: linear-gradient(to right, #00bfff, #ff6347);
border-radius: 5px;
height: 15px;
outline: none;
transition: background 0.3s ease-in-out;
}
.time-tuner::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
background: #fff;
border-radius: 50%;
border: 2px solid #333;
cursor: pointer;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
transition: transform 0.2s;
}
.time-tuner:focus::-webkit-slider-thumb {
transform: scale(1.2);
}
.tuner-display {
margin-top: 20px;
font-size: 24px;
background: rgba(255,255,255,0.7);
padding: 10px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
backdrop-filter: blur(5px);
}
This styling retains accessibility, ensuring labels and transformations are intuitive and engaging. The slider thumb expands upon focus, using transform: scale to highlight interactive potential.
This Time Tuner transforms mundane time input tasks into a nostalgic experience, drawing users into their interaction. It not only revives a retro style but marries form and function harmoniously. Builders and designers alike can integrate this inventive component into web projects, making time selection both useful and enchanting.
<div class="time-tuner-container">
<label for="time-tuner" class="time-tuner-label">Select Time</label>
<input type="range" id="time-tuner" class="time-tuner" min="0" max="1440" step="1" oninput="updateTunerDisplay()" aria-label="Time tuner" />
<div class="tuner-display">
<span class="current-time">12:00 AM</span>
</div>
</div>
<script>
function updateTunerDisplay() {
const tuner = document.getElementById('time-tuner');
const display = document.querySelector('.current-time');
const minutes = parseInt(tuner.value);
const hours = Math.floor(minutes / 60);
const mins = minutes % 60;
const period = hours >= 12 ? 'PM' : 'AM';
const adjustedHour = (hours % 12) || 12;
const formattedTime = adjustedHour + ':' + (mins < 10 ? '0' : '') + mins + ' ' + period;
display.textContent = formattedTime;
}
</script>
<style>
.time-tuner-container {
display: flex;
flex-direction: column;
align-items: center;
font-family: 'Courier New', Courier, monospace;
width: 100%;
max-width: 500px;
margin: auto;
}
.time-tuner-label {
font-size: 20px;
margin-bottom: 10px;
}
.time-tuner {
-webkit-appearance: none;
width: 100%;
background: linear-gradient(to right, #00bfff, #ff6347);
border-radius: 5px;
height: 15px;
outline: none;
transition: background 0.3s ease-in-out;
}
.time-tuner::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
background: #fff;
border-radius: 50%;
border: 2px solid #333;
cursor: pointer;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
transition: transform 0.2s;
}
.time-tuner:focus::-webkit-slider-thumb {
transform: scale(1.2);
}
.tuner-display {
margin-top: 20px;
font-size: 24px;
background: rgba(255,255,255,0.7);
padding: 10px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
backdrop-filter: blur(5px);
}
</style>
Thank you for reading this article.
Comments
No comments yet. Be the first to comment!