create a spinning loading cicrle in the center of the website in html and css and js
- User
create a spinning loading cicrle in the center of the website in html and css and js
- Assistant
- User
blur the page behind while loading
- Assistant
create a spinning loading cicrle in the center of the website in html and css and js
blur the page behind while loading
Creating a spinning loading circle can be achieved using a combination of HTML, CSS, and JavaScript. Here is a simple example to demonstrate how you can implement this on a webpage:
Create a basic HTML structure with a container for the loading spinner.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Loading Spinner</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="loading-container">
<div class="spinner"></div>
</div>
<script src="script.js"></script>
</body>
</html>
Use CSS to style the spinner and center it on the page. You can use keyframe animations to create a spinning effect.
/* styles.css */
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: #f3f3f3;
}
#loading-container {
display: flex;
align-items: center;
justify-content: center;
}
.spinner {
border: 8px solid #f3f3f3; /* Light grey */
border-top: 8px solid #3498db; /* Blue */
border-radius: 50%;
width: 60px;
height: 60px;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
If you want to control the visibility of the loading spinner (e.g., hide it after a certain event), you can do so with JavaScript.
// script.js
// Example: Hide the spinner after 3 seconds
window.onload = function() {
setTimeout(() => {
const loadingContainer = document.getElementById('loading-container');
loadingContainer.style.display = 'none'; // Hide the spinner
}, 3000); // 3000 milliseconds = 3 seconds
};
HTML: The spinner is contained within a div with the class "spinner". Another div ("loading-container") is used to help with centering this spinner on the page.
CSS:
spinner class creates the appearance of a circle using border styling. Only the top border is colored to achieve the spinning effect.@keyframes spin animation is used to rotate the spinner continuously.JavaScript: This optional script, included as an example, automatically hides the spinner after a specified duration (3 seconds in this case).
Feel free to customize the size, colors, and timing to better suit your project's needs!
To blur the page content behind the loading spinner, you can use the CSS backdrop-filter property. Below is an extended version of the previous example that incorporates a blurring effect when the spinner is displayed.
The HTML structure remains essentially the same.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Loading Spinner</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="content">
<h1>Welcome to My Website</h1>
<p>This is a sample page content that will be blurred while loading.</p>
</div>
<div id="loading-container">
<div class="spinner"></div>
</div>
<script src="script.js"></script>
</body>
</html>
Add styles to blur the entire page content except the loading spinner.
/* styles.css */
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden; /* Prevent scrolling while loading */
display: flex;
align-items: center;
justify-content: center;
background-color: #f3f3f3;
position: relative;
}
.content {
filter: blur(5px); /* Initial blur on the content */
transition: filter 0.5s ease-out;
}
#loading-container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
backdrop-filter: blur(5px); /* Blur effect */
background-color: rgba(255, 255, 255, 0.7); /* Semi-transparent background */
z-index: 1000; /* Ensure spinner is on top */
}
.spinner {
border: 8px solid #f3f3f3; /* Light grey */
border-top: 8px solid #3498db; /* Blue */
border-radius: 50%;
width: 60px;
height: 60px;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
Hide the loading spinner and remove the blur from the content once the page is loaded or after a specific duration.
// script.js
window.onload = function() {
setTimeout(() => {
const loadingContainer = document.getElementById('loading-container');
const content = document.querySelector('.content');
loadingContainer.style.display = 'none'; // Hide the spinner
content.style.filter = 'none'; // Remove the blur effect
}, 3000); // 3000 milliseconds = 3 seconds
};
CSS:
backdrop-filter property in #loading-container adds a blur effect to the backdrop. The background-color with a level of transparency enhances the visual separation between the content and the loading spinner.filter with blur is applied to the .content initially to blur the entire page content.filter style will transition smoothly back to normal when the blur is removed.JavaScript:
This setup effectively blurs the page behind the loading spinner and automatically restores it once the spinner is hidden, creating a focused and modern loading experience.
Not yet included in a published release.