write an html js sand hourglass sand timer
- User
write an html js sand hourglass sand timer
- Assistant
write an html js sand hourglass sand timer
Certainly! Here's an example of an HTML and JavaScript sand hourglass timer. This implementation uses HTML, CSS, and JavaScript to create an hourglass shape and animate the sand flowing from the top chamber to the bottom chamber over a specified duration.
You can adjust the duration of the timer by changing the parameter in the startTimer(duration) function.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sand Hourglass Timer</title>
<style>
/* Hourglass container */
#hourglass {
position: relative;
width: 100px;
height: 200px;
margin: 50px auto;
/* Create the hourglass shape using clip-path */
background: linear-gradient(to bottom, #ccc 50%, #fff 50%);
border: 2px solid #000;
clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
overflow: hidden;
}
/* Top sand */
#top-sand {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 50%;
background-color: gold;
transition: height linear;
}
/* Bottom sand */
#bottom-sand {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 0%;
background-color: gold;
transition: height linear;
}
/* Sand stream in the middle */
#stream {
position: absolute;
top: 50%;
left: 50%;
width: 4px;
height: 0;
background-color: gold;
/* Center the stream horizontally */
transform: translateX(-50%);
transition: height linear;
}
</style>
</head>
<body>
<div id="hourglass">
<div id="top-sand"></div>
<div id="bottom-sand"></div>
<div id="stream"></div>
</div>
<script>
function startTimer(duration) {
var topSand = document.getElementById('top-sand');
var bottomSand = document.getElementById('bottom-sand');
var stream = document.getElementById('stream');
// Reset the sand levels and stream height
topSand.style.height = '50%';
bottomSand.style.height = '0%';
stream.style.height = '0';
// Force reflow to apply initial styles before starting the transition
topSand.offsetHeight;
// Calculate the transition duration
var durationStr = duration + 's';
// Apply transition durations
topSand.style.transition = 'height ' + durationStr + ' linear';
bottomSand.style.transition = 'height ' + durationStr + ' linear';
stream.style.transition = 'height ' + durationStr + ' linear';
// Start the animation
topSand.style.height = '0%';
bottomSand.style.height = '50%';
stream.style.height = '50%';
}
// Start the timer with a duration of 10 seconds
startTimer(10);
</script>
</body>
</html>
Explanation:
HTML Structure:
#hourglass div acts as the container for the hourglass.#top-sand: Represents the sand in the top chamber.#bottom-sand: Represents the sand in the bottom chamber.#stream: Represents the stream of sand flowing between the chambers.CSS Styling:
clip-path to define a polygon that resembles an hourglass.background property uses a gradient to give a visual distinction between the top and bottom halves.height property linearly.JavaScript Animation:
startTimer function initializes the heights of the sand and sets up the transitions.#top-sand, #bottom-sand, and #stream are animated over the specified duration to simulate sand flowing from the top to the bottom.offsetHeight property is accessed to force the browser to reflow and recognize the initial styles before starting the animation.Customizing the Timer:
startTimer function in seconds. For example, to set a 30-second timer, use startTimer(30);.Note: This is a simple visual representation and does not include functionality to perform actions when the timer ends. If you want to trigger events when the timer completes, you can use setTimeout in conjunction with the duration.
// Start the timer with a 15-second duration
startTimer(15);
// Execute a function when the timer ends
setTimeout(function() {
alert('Time is up!');
}, 15000); // Duration in milliseconds
Feel free to integrate this code into your project and modify it to suit your needs. Let me know if you need any further assistance!