目錄
? Simple CSS Spinner (Loader)
✅ HTML
✅ CSS
? How It Works
?️ Optional: Centering the Loader on Screen
? Use Cases
首頁 web前端 css教學 CSS加載器或旋轉器示例

CSS加載器或旋轉器示例

Jul 27, 2025 am 03:06 AM

這是一個純CSS實現的輕量級加載動畫,1. 使用HTML創建一個div容器;2. 通過CSS設置圓形邊框並僅對頂部著色;3. 利用@keyframes動畫持續旋轉元素;4. 可自定義大小、速度和顏色;5. 通過Flex佈局居中顯示;6. 結合JavaScript動態控制顯示與隱藏,適用於表單提交、數據加載等場景,完整且無需外部依賴。

css loader or spinner example

Here's a simple and practical CSS loader (spinner) example you can use in your web projects. It's lightweight, pure CSS, and easy to customize.

css loader or spinner example

? Simple CSS Spinner (Loader)

This spinner is great for showing loading states while content loads (eg, API calls, page transitions).

✅ HTML

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

✅ CSS

 .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; /* Center it */
}

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

? How It Works

  • The spinner uses a circular border with only the top colored — this creates the "spinning" effect.
  • animation: spin 1s linear infinite rotates the element continuously.
  • You can change:
    • Size: adjust width / height
    • Speed: change 1s to faster ( 0.5s ) or slower
    • Colors: modify border and border-top values

?️ Optional: Centering the Loader on Screen

To center it vertically and horizontally:

css loader or spinner example
 body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
}

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

? Use Cases

  • Show during form submission
  • While fetching data from an API
  • On page navigation (single-page apps)
  • Inside buttons (eg, "Loading...")

You can wrap it in a container and hide it when not needed:

 .loader-container {
  display: none; /* Show dynamically with JS */
}

.loader-container.active {
  display: block;
}

That's it — a clean, modern spinner with no JavaScript or external libraries needed. Basically just a few lines of CSS.

css loader or spinner example

以上是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 17, 2025 am 07:30 AM

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

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

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

如何使用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 15, 2025 am 05:33 AM

使用border-radius:50%將等寬高的圖像變為圓形,結合object-fit和aspect-ratio確保形狀和裁剪,可添加邊框、陰影等樣式增強視覺效果。

如何在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