目录
1. Basic Structure (HTML)
2. Styling the Loader (CSS)
How It Works
3. Customization Tips
Example: Pulsing Dot Loader
4. Make It Responsive
Final Notes
首页 web前端 css教程 如何创建仅CSS的动画加载程序?

如何创建仅CSS的动画加载程序?

Sep 20, 2025 am 03:22 AM

创建一个仅使用HTML和CSS的动画加载器,首先在HTML中添加一个基础元素:

<div class="loader"></div>;2. 在CSS中通过设置宽高、边框、圆角和动画属性来实现旋转效果,利用border-top颜色差异和@keyframes从0到360度持续旋转;3. 可通过调整尺寸、动画速度、颜色或形状来自定义加载器样式,例如改为脉动圆点;4. 使用em/rem单位和Flex布局确保响应式适配;该方法无需JavaScript,轻量且兼容现代浏览器,适用于小型项目或备用加载状态。

How to create a CSS-only animated loader?

Creating a CSS-only animated loader is a lightweight and effective way to show loading states without JavaScript. Here's how to make a simple spinning loader using just HTML and CSS.

How to create a CSS-only animated loader?

1. Basic Structure (HTML)

Start with a minimal HTML structure. You only need one element:

<div class="loader"></div>

This div will represent the loader on the page.

How to create a CSS-only animated loader?

2. Styling the Loader (CSS)

Use CSS to create a circular spinner with animation. Here's a clean, modern loader:

.loader {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 50px auto; /* for centering */
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

How It Works

  • Border technique: The loader uses a transparent interior with a light gray border all around, except the top (which is colored). This creates the "half-circle" effect.
  • border-radius: 50%: Turns the square div into a perfect circle.
  • animation: spin 1s linear infinite: Applies a continuous rotation.
  • @keyframes spin: Rotates the element from 0 to 360 degrees repeatedly.

3. Customization Tips

You can easily tweak the appearance:

How to create a CSS-only animated loader?
  • Size: Adjust width and height.
  • Speed: Change 1s in the animation to something faster (0.5s) or slower (2s).
  • Colors: Modify border and border-top colors.
  • Shape: Use border-radius: 0 and animate scale or opacity for a different effect (e.g., pulsing bar).

Example: Pulsing Dot Loader

.loader {
  width: 15px;
  height: 15px;
  background: #333;
  border-radius: 50%;
  animation: pulse 0.8s ease-in-out infinite;
  margin: 50px auto;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(0);
    opacity: 0;
  }
  50% {
    transform: scale(1);
    opacity: 1;
  }
}

4. Make It Responsive

To ensure it works well in different containers:

  • Use em or rem units for scalability.
  • Center with display: flex if inside a wrapper:
.loader-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

Final Notes

  • No JavaScript required.
  • Lightweight and fast-rendering.
  • Works on all modern browsers.
  • Ideal for small projects or fallback loading states.

Basically, just define a shape, apply a smooth animation using @keyframes, and let CSS do the rest.

以上是如何创建仅CSS的动画加载程序?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Stock Market GPT

Stock Market GPT

人工智能驱动投资研究,做出更明智的决策

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

如何使用纯CSS创建下拉菜单 如何使用纯CSS创建下拉菜单 Sep 20, 2025 am 02:19 AM

使用HTML和CSS可创建无需JavaScript的下拉菜单。2.通过:hover伪类触发子菜单显示。3.利用嵌套列表构建结构,CSS设置隐藏与悬浮显示效果。4.可添加过渡动画提升视觉体验。

如何防止图像拉伸或缩小CSS 如何防止图像拉伸或缩小CSS Sep 21, 2025 am 12:04 AM

useobject-fitormax-widthwithheight:自动置换式; object-fitControlshowimagesfillcontainersfillcontainerswhilepreservingaspectratios,andmax-width:100%;高度;高度:autoEsoensuresResresresResresRessersRessiveScalingScalingWithOutStertracterging。

如何使用CSS中的指针事件属性 如何使用CSS中的指针事件属性 Sep 17, 2025 am 07:30 AM

Thepointer-eventspropertyinCSScontrolswhetheranelementcanbethetargetofpointerevents.1.Usepointer-events:nonetodisableinteractionslikeclicksorhoverswhilekeepingtheelementvisuallyvisible.2.Applyittooverlaystoallowclick-throughbehaviortounderlyingelemen

如何使用CSS添加盒子阴影效果 如何使用CSS添加盒子阴影效果 Sep 20, 2025 am 12:23 AM

USETHEBOX-SHADOWPROPERTYTOADDDROPSHADOWS.DEFINEHORIZONTALANDVERTICALESTESETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETESTESTESTESTESTESTEMENG:MMULTIPLESHADOWSARECOMMA-SEPARAWS.MEULTIPLESHADOWSARECOMMA-SEPARATED.EXAMPL

如何将过滤器应用于CSS的图像 如何将过滤器应用于CSS的图像 Sep 21, 2025 am 02:27 AM

thecssfilterpropertyallowseasyagestylinglingwisslikeblur,亮度和格雷斯卡尔(Grayscale.UseFilter):滤波器函数(值)onimagesorbackgroundImages.commonfunctionsIncludeBlurblur(px),亮度(brightness),亮度(%),偏见(%),损坏(%),sancale(%),饱和度(%)

如何在CSS中添加梯度背景 如何在CSS中添加梯度背景 Sep 16, 2025 am 05:30 AM

要添加CSS渐变背景,使用background或background-image属性配合linear-gradient()、radial-gradient()等函数即可;首先选择渐变类型,设置方向与颜色,并可通过颜色停靠点、形状、大小等参数精细控制,例如linear-gradient(toright,#ff7e5f,#feb47b)创建从左到右的线性渐变,radial-gradient(circle,#ff9a9e,#fecfef)创建圆形径向渐变,还可通过repeating-linear-gr

如何在CSS列表上创建交错的动画效果 如何在CSS列表上创建交错的动画效果 Sep 18, 2025 am 12:15 AM

使用CSS创建交错动画效果需为列表项设置相同动画但错开开始时间。首先构建无序列表HTML结构,接着定义如淡入上滑的@keyframes动画,然后通过:nth-child选择器或CSS自定义属性为每个列表项设置递增的animation-delay实现stagger效果,最后可选JavaScript控制进入视口时触发。该方法通过协调元素时序实现自然流畅的级联动画。

如何在CSS网格布局中创建空白? 如何在CSS网格布局中创建空白? Sep 22, 2025 am 05:15 AM

使用gap、row-gap或column-gap属性可在CSSGrid布局中创建网格项之间的间距,gap是设置行列间距的简写属性,可接受一个或两个长度值,row-gap和column-gap则分别单独控制行与列的间距,支持px、rem、%等单位。

See all articles