我试图在JavaScript中创建一个GIF API。每次我搜索另一个GIF时,它只是添加到我以前搜索的GIF中,除非我刷新它。我尝试在forEach
代码块开始之前使用imagesEl.innerHTML = '';
,但我意识到它不起作用。我还尝试在结束forEach
代码块后使用if语句,但它不起作用,或者可能是因为我编码错误。任何评论都会有帮助!我很抱歉,如果我的一些句子是不正确的,我清楚地知道,我应该解释好他们的准确解决方案,这是因为英语不是我的第一语言。
const formEl = document.querySelector('form');
const loaderEl = document.querySelector('.loader');
const mainContainer = document.querySelector('.container')
const imgContainer = document.querySelector('.img-container')
function showLoader() {
loaderEl.style.display = 'block';
}
function hideLoader() {
loaderEl.style.display = 'none';
loaderEl.innerHTML = '';
}
formEl.addEventListener('submit', (e) => {
e.preventDefault();
const input = formEl.querySelector('input[name="input"]').value;
if (input.length >= 1){
showLoader();
performSearch(input);
} else {
hideLoader(input);
}
e.target.reset();
});
function performSearch(search) {
const gifCount = 10;
const apiKey = 'VL071LzBWMdYPAyxErdI1d0YPs406vB5';
const url = `https://api.giphy.com/v1/gifs/search?api_key=${apiKey}&q=${search}&limit=${gifCount}&offset=0&rating=pg-13&lang=en&bundle=messaging_non_clips`;
fetch(url)
.then((response) => response.json())
.then((info) => {
console.log(info.data)
const giftData = info.data;
giftData.forEach((e) => {
// imagesEl.innerHTML = '';
//create a div with classlist of images
const imagesEl = document.createElement('div');
imagesEl.classList.add('images');
//inside images div create img to store the gifs.
const img = document.createElement('img');
img.setAttribute("src", e.images.fixed_height_downsampled.url);
// append all
imagesEl.appendChild(img);
imgContainer.appendChild(imagesEl);
//if(e > 10){
imagesEl.innerHTML = '';}
hideLoader();
});
});
};
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-color: #7C9D96;
display: flex;
align-items: center;
justify-content: center;
font-size: 'Roboto';
}
.container {
position: relative;
align-items: center;
flex-direction: column;
max-width: 1600px;
height: 100vh;
margin: auto;
padding: 50px 0;
}
input {
height: 80px;
width: 800px;
font-size: 20px;
padding-left: 10px;
border: none;
outline: none;
}
input:focus {
color: #353535;
}
.search-container {
margin: auto 50px;
width: 100%;
}
.btn {
height: 80px;
width: 200px;
border: none;
cursor: pointer;
font-size: 20px;
color: #353535;
}
.btn:active {
background-color: #749BC2;
transition: .3s;
}
.loader {
position: absolute;
top: 40%;
left: 40%;
display: none;
width: 200px;
height: 200px;
border: 30px solid #0B666A;
border-radius: 50%;
border-bottom: 30px solid white;
animation: rotate .3s infinite linear;
}
@keyframes rotate {
0% {
rotate: 0deg;
}
100% {
rotate: 360deg;
}
}
.img-container {
width: 100%;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
margin: 20px;
align-items: center;
justify-content: center;
}
.images {
display: flex;
align-items: center;
justify-content: center;
padding: 5px;
border-radius: 5px;
margin: 15px;
padding: 20px;
background-color: #F4F2DE;
}
.images:hover {
padding-bottom: 50px;
transition: .5s;
}
.images:hover:after{
transition: .5s;
}
.images img {
width: 100%;
object-fit: contain;
height: 150px;
}
<div class="container">
<div class="search-container">
<form>
<input type="text" name="input" class="text" placeholder="Ex. Happy" autocomplete="off">
<button class="btn">Search</button>
</form>
</div>
<div class="loader"></div>
<div class="img-container">
<!-- <div class="images">
<img src="https://picsum.photos/id/137/200/300" alt="">
</div> -->
</div>
</div>
1条答案
按热度按时间fdbelqdn1#
这是半psudo代码,但概述了需要发生的事情。在使用子元素填充div之前,应该清除div。如果e<10,你不能把e作为整数来计算。列表的长度已经达到10,只需遍历它并创建img元素。:D个
字符串