Published on Feb 27, 2024
During today’s lesson, we will examine the development of a user-friendly stopwatch application. We’ll examine the HTML, CSS, and JavaScript code that powers this stylish stopwatch. Let’s explore this modern interface.
The document starts with the ‘html’ tag, indicating that this is an HTML document. The ‘lang’ attribute specifies the language as English.
Next, we have the ‘head’ section where meta information and the document title are defined.
The ‘meta’ tags specify the character set and the viewport settings for the document, ensuring proper display on various devices.
Here, the ‘title’ tag sets the title of the document as ‘Stopwatch‘.
Moving on to the ‘body’ section, this is where the visible content of the document is defined.
Inside the body, we have a ‘div’ element with the class ‘box’, which likely contains the entire stopwatch interface.
Within the box, there’s another div with the class ‘stopwatch’ which is where the time display will appear. It currently shows ’00:00:00′.
Below the time display, we have another div with the class ‘controls’ containing the buttons for controlling the stopwatch.
There are three buttons: ‘Start’, ‘Stop’, and ‘Reset’, which likely correspond to starting, stopping, and resetting the stopwatch.
And that’s the breakdown of the HTML code for this stopwatch application.
Now that we’ve covered the HTML structure of our stopwatch, let’s move on to the CSS part to make the user interface more user-friendly.
The CSS code here defines the visual style and layout of the stopwatch interface.
The ‘body’ selector sets the styling for the entire page. It specifies that the font should be Arial or a sans-serif fallback, the text should be centered, and the background colour should be white. The margin and padding are set to 0.
“The ‘.stopwatch’ class styles the time display. It sets the width, margin, font size, background colour, text colour, border radius, padding, and box shadow, creating a visually appealing display for the time.
Next, the ‘.controls’ class provides styling for the buttons. It sets the top margin and positions the buttons below the time display.
Within the ‘.button’ class, we define the styling for the buttons. This includes font size, padding, margin, background colour, text colour, border, border radius, and cursor upon hover. The transition property creates a smooth colour change on hover, and the box shadow adds a subtle 3D effect.
As you can see, when a button is hovered over, the background colour smoothly transitions to a lighter shade, enhancing the interactive feel of the buttons.
Finally, the ‘.box’ class styles the container holding the stopwatch and controls. It sets the width, height, background colour, margin, border radius, box shadow, and position. This creates a visually appealing and well-positioned container for the stopwatch.
And that’s a breakdown of the CSS code, illustrating how it enhances the user experience of our stopwatch interface.
After completing the styling with CSS, let’s move on to the JavaScript part to bring our stopwatch to life and make the user interface even more user-friendly.
The JavaScript code here utilises the jQuery library to add interactivity to the stopwatch interface.
The ‘document.ready’ function ensures that the JavaScript code within it will run when the HTML document is fully loaded.
Here, we initialise variables for the stopwatch, running state, and time.
The ‘updateTime’ function calculates the hours, minutes, and seconds based on the ‘time’ variable, and updates the text displayed in the stopwatch accordingly.
The ‘startTimer’ function initiates the setInterval method to increment the ‘time’ variable by one second periodically and updates the display using the ‘updateTime’ function.
Conversely, the ‘stopTimer’ function stops the interval set by ‘startTimer’ using the clearInterval method when the stopwatch is paused.
The ‘resetTimer’ function stops the timer, resets the ‘time’ variable to zero, and updates the stopwatch display to reflect the reset time.
Lastly, event handlers are set for the ‘Start’, ‘Stop’, and ‘Reset’ buttons, which trigger the corresponding functions to control the stopwatch based on the user’s interactions.
And that’s an overview of the JavaScript code, demonstrating how it adds functionality and responsiveness to our stopwatch interface.
As a result, you will find the Stop Watch ready for use at the end of this procedure. For regular updates, please hit the like button and subscribe to our YouTube channel.