HTML5搜索框加载动画怎么实现
时间:2026-01-11 12:42:40 340浏览 收藏
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《HTML5搜索框加载动画实现方法》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。
为HTML5搜索框添加loading动画有四种方法:一、CSS旋转动画配合伪元素;二、SVG内联加载图标;三、CSS自定义属性控制动画开关;四、纯CSS无JS状态模拟。

当用户在搜索框中输入关键词并触发搜索请求时,页面可能需要等待后端响应,此时添加加载动画能提升用户体验。以下是为HTML5搜索框添加loading动画的多种方法:
一、使用CSS旋转动画配合伪元素
该方法通过CSS定义一个旋转的环形动画,并利用伪元素(::after)动态插入到搜索框右侧,在搜索进行时显示,无需额外DOM节点。
1、在HTML中定义搜索框,添加class标识:<input type="search" id="searchInput" class="search-with-loading" placeholder="请输入关键词">
2、在CSS中定义旋转动画及加载状态样式:@keyframes spin { to { transform: rotate(360deg); } }
.search-with-loading.loading::after { content: ""; display: inline-block; width: 16px; height: 16px; border: 2px solid #ddd; border-top-color: #007bff; border-radius: 50%; animation: spin 0.8s linear infinite; margin-left: 8px; }
3、在JavaScript中监听搜索事件,在发起请求前添加loading类,响应完成后移除:document.getElementById("searchInput").addEventListener("input", function() {
this.classList.add("loading");
fetch("/search?q=" + this.value)
.then(() => this.classList.remove("loading")); });
二、在搜索框内部插入SVG内联加载图标
该方法将轻量级SVG作为内联元素嵌入input容器,通过CSS控制其显隐,确保动画独立于文本内容且适配高DPI屏幕。
1、用div包裹搜索框与SVG图标,避免input直接嵌套SVG:
<input type="search" id="searchInput2" placeholder="搜索...">
2、为SVG路径添加描边动画:.loading-path { stroke-dasharray: 15; stroke-dashoffset: 0; animation: dash 1.5s ease-in-out infinite; }
@keyframes dash { 0% { stroke-dashoffset: 0; } 50% { stroke-dashoffset: -5; } 100% { stroke-dashoffset: 0; } }
3、通过JavaScript切换容器的loading状态类来控制图标显隐:const container = document.querySelector(".search-container");
container.addEventListener("submit", function(e) {
e.preventDefault();
container.classList.add("loading");
fetch("/api/search").then(() => container.classList.remove("loading")); });
三、使用CSS自定义属性控制动画开关
该方法利用CSS自定义属性(--loading)作为开关变量,结合transition实现平滑的加载态过渡,避免强制重排,性能更优。
1、在CSS中定义基于自定义属性的条件样式:input.search-ctrl { position: relative; padding-right: 32px; }
input.search-ctrl::after { content: ""; position: absolute; right: 8px; top: 50%; transform: translateY(-50%); width: 12px; height: 12px; background: conic-gradient(from 0deg, #007bff, #007bff 25%, transparent 25%); background-size: 200% 200%; transition: background-position 0.3s; }
input.search-ctrl[data-loading="true"]::after { background-position: 0 100%; animation: rotate 1s linear infinite; }
2、在JavaScript中通过dataset设置data-loading属性:const searchBox = document.getElementById("searchInput3");
searchBox.addEventListener("keydown", function(e) {
if (e.key === "Enter") {
searchBox.dataset.loading = "true";
fetch("/search?q=" + searchBox.value).then(() => delete searchBox.dataset.loading);
}
});
3、补充@keyframes规则以支持旋转:@keyframes rotate { from { transform: translateY(-50%) rotate(0deg); } to { transform: translateY(-50%) rotate(360deg); } }
四、采用纯CSS实现无JS的搜索状态模拟
该方法适用于静态搜索或仅需视觉反馈的场景,利用:focus-within和:checked等伪类,不依赖JavaScript即可呈现加载态。
1、构建包含隐藏单选按钮的搜索结构:
2、设置label为绝对定位的加载指示器,并在radio被JS选中时显示:.search-form { position: relative; }
.loading-indicator { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); width: 14px; height: 14px; border: 2px solid transparent; border-top-color: #007bff; border-radius: 50%; }
.state-radio:checked ~ .loading-indicator { animation: spin 0.6s linear infinite; }
3、在搜索触发逻辑中调用radio.click()激活状态:document.getElementById("searchInput4").addEventListener("input", function() {
if (this.value.length > 2) {
document.getElementById("loadingRadio").click();
// 实际请求完成后需手动取消选中
}
});
今天关于《HTML5搜索框加载动画怎么实现》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
316 收藏
-
280 收藏
-
341 收藏
-
347 收藏
-
230 收藏
-
428 收藏
-
132 收藏
-
335 收藏
-
108 收藏
-
370 收藏
-
339 收藏
-
332 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习