我试图创建一个在网络调用期间旋转的微调器,但它不起作用我尝试使用 classList add 方法来添加/删除其中设置了 display :hidden 属性的隐藏类
**HTML **
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meme Generator</title>
<link rel="icon" type="image/x-icon" href="/TrollFace.png">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
<div class="container">
<header>
<h1>Meme Generator</h1>
</header>
<main>
<button class="button-5" id="generateMemeBtn" role="button">Generate Meme</button>
<div id="memeContainer">
<div id="loadingSpinner" class="spinner hidden"></div>
<h1>Hello there👋 !!</h1>
<img src="./hi-hello.gif" alt="">
</div>
</main>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
body {
font-family: 'Roboto', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #e9ecef;
margin: 0;
}
.container {
text-align: center;
background-color:#f8f9fa;
padding: 40px;
border-radius: 4px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
height:75%;
width: 90%;
max-width: 600px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
header {
margin-bottom: 20px;
}
header h1 {
margin: 0;
font-size: 2.5em;
color: #212529;
}
.button-5 {
margin: 10px;
align-items: center;
background-clip: padding-box;
background-color: #d00000;
border: 1px solid transparent;
border-radius: .25rem;
box-shadow: rgba(0, 0, 0, 0.02) 0 1px 3px 0;
box-sizing: border-box;
color: #fff;
cursor: pointer;
display: inline-flex;
font-family: system-ui,-apple-system,system-ui,"Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 16px;
font-weight: 600;
justify-content: center;
line-height: 1.25;
min-height: 3rem;
padding: calc(.875rem - 1px) calc(1.5rem - 1px);
position: relative;
text-decoration: none;
transition: all 250ms;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
vertical-align: baseline;
width: auto;
}
.button-5:hover {
background-color: #d00000;
box-shadow: rgba(255, 255, 255, 0.4) 0 4px 12px;
}
.button-5:hover {
transform: translateY(-1px);
}
.button-5:active {
background-color: #9c0404;
box-shadow: rgba(0, 0, 0, .06) 0 2px 4px;
transform: translateY(0);
}
#memeContainer {
margin-top: 20px;
position: relative;
min-height: 200px;
height: 60vh;
width: 38vw;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: #212529;
}
#memeContainer img {
max-width: 100%;
max-height: 90%;
border-radius: 5px;
box-shadow: 0 7px 20px rgba(0, 0, 0, 0.3);
}
.spinner {
position:absolute;
top: 50%;
left: 50%;
z-index: 100;
width: 48px;
height: 48px;
border: 5px solid #FFF;
border-bottom-color: #FF3D00;
border-radius: 50%;
display: inline-block;
box-sizing: border-box;
animation: rotation 1s linear infinite;
}
.hidden{
display: none;
}
@keyframes rotation {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
JS
document.addEventListener('DOMContentLoaded', () => {
const generateMemeBtn = document.getElementById('generateMemeBtn');
const memeContainer = document.getElementById('memeContainer');
const loadingSpinner = document.getElementById('loadingSpinner');
generateMemeBtn.addEventListener('click', memeApi);
function memeApi()
{
loadingSpinner.classList.remove('hidden');
console.log(loadingSpinner.className)
memeContainer.innerHTML = '';
const url = 'https://meme-api.com/gimme/15';
fetch(url)
.then(data => data.json() )
.then(data => {
const memes = data;
const randomIndex = Math.floor(Math.random() * memes.count);
const meme = memes.memes[randomIndex];
loadingSpinner.classList.add('hidden');
memeContainer.innerHTML = `
<h2>${meme.title}</h2>
<img src="${meme.url}" alt="${meme.author}">
`;
})
.catch(error => {
console.error('Error fetching meme:', error);
loadingSpinner.classList.add('hidden');
memeContainer.innerHTML = '<p>Sorry, something went wrong. Please try again later.</p>';
});
}
});
我期待一个滑块在 API 获取期间旋转,然后消失。当结果出来的时候。
您需要做的就是将微调器 div 移到容器 div 之外,这样微调器在加载时就不会被 meme 替换。
document.addEventListener('DOMContentLoaded', () => {
const generateMemeBtn = document.getElementById('generateMemeBtn');
const memeContainer = document.getElementById('memeContainer');
const loadingSpinner = document.getElementById('loadingSpinner');
generateMemeBtn.addEventListener('click', memeApi);
function memeApi()
{
loadingSpinner.classList.remove('hidden');
console.log(loadingSpinner.className)
memeContainer.innerHTML = '';
const url = 'https://meme-api.com/gimme/15';
fetch(url)
.then(data => data.json() )
.then(data => {
const memes = data;
const randomIndex = Math.floor(Math.random() * memes.count);
const meme = memes.memes[randomIndex];
loadingSpinner.classList.add('hidden');
memeContainer.innerHTML = `
<h2>${meme.title}</h2>
<img src="${meme.url}" alt="${meme.author}">
`;
})
.catch(error => {
console.error('Error fetching meme:', error);
loadingSpinner.classList.add('hidden');
memeContainer.innerHTML = '<p>Sorry, something went wrong. Please try again later.</p>';
});
}
});
body {
font-family: 'Roboto', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #e9ecef;
margin: 0;
}
.container {
text-align: center;
background-color:#f8f9fa;
padding: 40px;
border-radius: 4px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
height:75%;
width: 90%;
max-width: 600px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
header {
margin-bottom: 20px;
}
header h1 {
margin: 0;
font-size: 2.5em;
color: #212529;
}
.button-5 {
margin: 10px;
align-items: center;
background-clip: padding-box;
background-color: #d00000;
border: 1px solid transparent;
border-radius: .25rem;
box-shadow: rgba(0, 0, 0, 0.02) 0 1px 3px 0;
box-sizing: border-box;
color: #fff;
cursor: pointer;
display: inline-flex;
font-family: system-ui,-apple-system,system-ui,"Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 16px;
font-weight: 600;
justify-content: center;
line-height: 1.25;
min-height: 3rem;
padding: calc(.875rem - 1px) calc(1.5rem - 1px);
position: relative;
text-decoration: none;
transition: all 250ms;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
vertical-align: baseline;
width: auto;
}
.button-5:hover {
background-color: #d00000;
box-shadow: rgba(255, 255, 255, 0.4) 0 4px 12px;
}
.button-5:hover {
transform: translateY(-1px);
}
.button-5:active {
background-color: #9c0404;
box-shadow: rgba(0, 0, 0, .06) 0 2px 4px;
transform: translateY(0);
}
#memeContainer {
margin-top: 20px;
position: relative;
min-height: 200px;
height: 60vh;
width: 38vw;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: #212529;
}
#memeContainer img {
max-width: 100%;
max-height: 90%;
border-radius: 5px;
box-shadow: 0 7px 20px rgba(0, 0, 0, 0.3);
}
.spinner {
position:absolute;
top: 50%;
left: 50%;
z-index: 100;
width: 48px;
height: 48px;
border: 5px solid #FFF;
border-bottom-color: #FF3D00;
border-radius: 50%;
display: inline-block;
box-sizing: border-box;
animation: rotation 1s linear infinite;
}
.hidden{
display: none;
}
@keyframes rotation {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
<div class="wrapper">
<div class="container">
<header>
<h1>Meme Generator</h1>
</header>
<main>
<button class="button-5" id="generateMemeBtn" role="button">Generate Meme</button>
<div id="memeContainer">
<h1>Hello there👋 !!</h1>
<img src="./hi-hello.gif" alt="">
</div>
<!-- Move your spinner out of the "memeContainer", so it doesn't get replaced by the meme -->
<div id="loadingSpinner" class="spinner hidden"></div>
</main>
</div>
</div>