要在JavaScript中显示微调器,您可以使用HTML、CSS和JavaScript来实现。以下是一个简单的示例,说明如何在加载图像时显示微调器。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Loader with Spinner</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="image-container">
<img id="myImage" src="https://via.placeholder.com/500x500" alt="Placeholder Image">
<div id="spinner" class="spinner"></div>
</div>
<script src="script.js"></script>
</body>
</html>
/* styles.css */
.image-container {
position: relative;
display: inline-block;
}
.spinner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 5px solid #f3f3f3;
border-top: 5px solid #3498db;
border-radius: 50%;
width: 50px;
height: 50px;
animation: spin 2s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
// script.js
const image = document.getElementById('myImage');
const spinner = document.getElementById('spinner');
image.addEventListener('load', () => {
spinner.style.display = 'none';
});
image.addEventListener('error', () => {
spinner.style.display = 'none';
alert('Image failed to load.');
});
这个示例将在图像加载时显示微调器,并在图像加载完成或加载失败时隐藏微调器。您可以根据需要调整样式和行为。
领取专属 10元无门槛券
手把手带您无忧上云