如何在添加新GIF前清除已搜索的历史GIF结果?
GIF搜索结果重复追加问题解决方法
问题原因
你之前尝试的imagesEl.innerHTML = '';无效,是因为这个imagesEl是每次循环新创建的div,清空它根本不会影响页面上已存在的旧结果。真正需要清空的是存放所有GIF的父容器——imgContainer。
修复步骤
- 在遍历GIF数据前,先清空
imgContainer的内容,确保新搜索结果不会和旧内容叠加 - 把
hideLoader()移到forEach循环外面,等所有GIF渲染完成后再隐藏加载器,避免加载器提前消失 - 删除循环里多余的
imagesEl.innerHTML = '';,这行代码会刚创建完GIF就把它清空,反而导致显示异常
修改后的完整代码
JavaScript
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(); } 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; // 关键:先清空容器内的旧内容 imgContainer.innerHTML = ''; giftData.forEach((e) => { // 创建存放单张GIF的容器 const imagesEl = document.createElement('div'); imagesEl.classList.add('images'); // 创建GIF图片元素 const img = document.createElement('img'); img.setAttribute("src", e.images.fixed_height_downsampled.url); // 将图片添加到容器,再把容器添加到页面的父容器中 imagesEl.appendChild(img); imgContainer.appendChild(imagesEl); }); // 所有GIF渲染完成后再隐藏加载器 hideLoader(); }); };
CSS
* { padding: 0; margin: 0; box-sizing: border-box; } body { background-color: #7C9D96; display: flex; align-items: center; justify-content: center; font-family: 'Roboto'; } .container { position: relative; display: flex; 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: 0 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 img { width: 100%; object-fit: contain; height: 150px; }
HTML
<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"> <!-- 动态GIF内容会在这里生成 --> </div> </div>
内容的提问来源于stack exchange,提问作者Gad Ashell
相关产品推荐
相关产品推荐

