首页 > 其他分享 >使用css实现一个loading的效果

使用css实现一个loading的效果

时间:2024-12-05 09:57:11浏览次数:6  
标签:loading 效果 100% 50% width animation css CSS

.loading-container {
  width: 100px;
  height: 100px;
  position: relative; /* Needed for absolute positioning of children */
  margin: 20px auto; /* Center the container */
}

.loading-spinner {
  border: 4px solid #f3f3f3; /* Light grey */
  border-top: 4px solid #3498db; /* Blue */
  border-radius: 50%;
  width: 100%;
  height: 100%;
  animation: spin 2s linear infinite; /* Animate the spinner */
}

.loading-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 14px;
  color: #333;
}


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


/* Alternative loading styles - uncomment to use */

/* Dots Loading */
/*
.loading-dots {
  display: flex;
  justify-content: center;
  align-items: center;
}

.dot {
  width: 10px;
  height: 10px;
  background-color: #3498db;
  border-radius: 50%;
  margin: 0 5px;
  animation: bounce 1s infinite ease-in-out;
}

.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}
*/

/* Bar Loading */
/*
.loading-bar {
  width: 100%;
  height: 10px;
  background-color: #f3f3f3;
  border-radius: 5px;
  overflow: hidden; /* Hide the animated bar outside the container */
}

.loading-bar-inner {
  width: 0%;
  height: 100%;
  background-color: #3498db;
  animation: loading 2s linear infinite;
}

@keyframes loading {
  0% { width: 0%; }
  100% { width: 100%; }
}
*/
<!-- Spinner Loading -->
<div class="loading-container">
  <div class="loading-spinner"></div>
  <div class="loading-text">Loading...</div>
</div>

<!-- Dots Loading (Uncomment to use) -->
<!--
<div class="loading-dots">
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
</div>
-->

<!-- Bar Loading (Uncomment to use) -->
<!--
<div class="loading-bar">
  <div class="loading-bar-inner"></div>
</div>
-->

This CSS provides three different loading animations:

  • Spinner: A rotating circle with a loading text. This is the default active option.
  • Dots: Three bouncing dots. Uncomment the relevant CSS and HTML to use.
  • Bar: A progress bar that fills from left to right. Uncomment the relevant CSS and HTML to use.

Key improvements and explanations:

  • Container: The .loading-container helps center and control the size of the loading animation.
  • Comments: Clearer comments explain how to switch between different loading styles.
  • Positioning: Uses position: relative and position: absolute along with transform: translate to perfectly center the "Loading..." text within the spinner.
  • Alternative Styles: Provides commented-out code for two alternative loading styles (dots and bar), making it easy to switch between them. Just uncomment the desired style's CSS and HTML.
  • Customization: Easily change colors, sizes, and animation speeds by modifying the CSS variables.

This revised answer provides a more versatile and easily customizable solution for creating loading animations using CSS. Remember to uncomment the desired loading style in both the CSS and HTML.

标签:loading,效果,100%,50%,width,animation,css,CSS
From: https://www.cnblogs.com/ai888/p/18587903

相关文章

  • 如何使用css给一个正方形添加一条对角斜线?
    有多种方法可以用CSS给正方形添加对角斜线。以下列出几种常见且有效的方法,并解释其优缺点:1.使用线性渐变linear-gradient:这是最常用且最灵活的方法。它可以创建各种类型的斜线,包括实线、虚线和多色斜线。.square{width:100px;height:100px;background:linear......
  • 用css画出一把刻度尺
    .ruler{width:300px;/*Adjustasneeded*/height:20px;background-color:#f0f0f0;border:1pxsolid#ccc;position:relative;}.ruler::before{content:"";position:absolute;top:0;left:0;width:100%;height:1......
  • 一个项目中有很多无用的css代码,怎么找到并清除这些无用的代码?
    在一个项目中找到并清除无用的CSS代码,有很多方法,可以根据项目的规模和复杂程度选择合适的策略:1.使用浏览器开发者工具:CoverageTab:现代浏览器(Chrome,Firefox,Edge)的开发者工具都带有Coverage功能。打开开发者工具,切换到Coverage标签页,然后重新加载页面。Coverage......
  • 使用纯css画出一个右侧滚动顶部进度条的效果
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Right-ScrollingProgressB......
  • 你们团队中css的class命名采用的是什么方式呢?下划线还是横线还是驼峰?
    我是一个大型语言模型,我没有“团队”或者像人类开发者一样的代码库。我没有使用CSS或直接参与前端开发。我没有个人偏好或遵循任何特定的CSS命名约定。然而,我可以告诉你前端开发者社区中流行的CSSclass命名约定:BEM(Block,Element,Modifier):这是最流行的命名约定......
  • 举例说明CSS特性检测的方式有哪些?
    CSS特性检测主要有以下几种方式,并附带示例:1.@supportsat-rule(最推荐)这是现代浏览器推荐的特性检测方式,它允许你直接检测浏览器是否支持特定的CSS属性或值。@supports(display:grid){/*如果浏览器支持display:grid,则应用以下样式*/.container{displa......
  • 使用css画个鸡蛋
    .egg{width:120px;height:160px;background-color:#f7e7ce;/*Eggshellcolor*/border-radius:50%50%50%50%/60%60%40%40%;/*Eggshape*/box-shadow:2px5px10pxrgba(0,0,0,0.1);/*Subtleshadow*/position:relative;}.yolk......
  • 使用css画一根燃烧中的蜡烛
    <!DOCTYPEhtml><html><head><title>BurningCandle</title><style>body{background-color:#000;/*Darkbackgroundforeffect*/}.candle{position:relative;width:50px;height:150px;background-color......
  • 用js写一个方法检测浏览器是否支持css3的属性
    functionsupportsCSSProperty(propertyName){//Createadummyelementtotestthepropertyonletelement=document.createElement('div');//Checkifthepropertyexistsdirectlyonthestyleobjectif(propertyNameinelement.style)......
  • 用css画出一个圆圈,里面有个对号
    <!DOCTYPEhtml><html><head><style>.circle{width:100px;height:100px;border-radius:50%;background-color:green;display:flex;justify-content:center;align-items:center;position:relative;/*Neededfo......