使用骨架加载器为您的网站带来专业的触感! 如果您从事前端开发,请忘记无聊的加载旋转器。骨架装载机提供了更具吸引力和更高效的用户体验。 在本教程中,您将学习如何使用 HTML 和 CSS 轻松创建一个,而不需要外部库。
?最终结果
开始之前,我们向您展示我们将获得的结果:
模拟图像、标题和一段文本加载的卡片。简单,但具有优雅和专业的外观。
?骨架式装载机的优点
- 更快的速度感:用户在加载时查看内容的表示,与简单的旋转器不同。
- 更精致的设计:提供内容预览,避免应用失败的感觉。
- 易于实现:使用纯 CSS 创建,没有额外的复杂性。
✍️ 让我们开始工作:创建加载器
我们将创建一个加载器来模仿带有图像、标题和文本的卡片。
1.基本 HTML 结构
首先创建具有以下结构的 HTML 文件:
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Skeleton Loader</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="card"> <!-- Contenido del loader aquí --> </div> <script src="script.js"></script> </body> </html>
2. CSS 样式
使用以下 CSS 代码创建文件 styles.css
:
body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f4f4f4; } .card { width: 300px; padding: 20px; border-radius: 8px; background-color: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .skeleton { background-color: #e0e0e0; border-radius: 4px; position: relative; overflow: hidden; } .skeleton::before { content: ''; display: block; position: absolute; top: 0; left: -100%; width: 100%; height: 100%; background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.4) 50%, rgba(255, 255, 255, 0) 100%); animation: shimmer 1.5s infinite; } @keyframes shimmer { 0% { left: -100%; } 100% { left: 100%; } } .skeleton.image { width: 100%; height: 150px; margin-bottom: 16px; } .skeleton.title { width: 70%; height: 20px; margin-bottom: 12px; } .skeleton.text { width: 100%; height: 14px; margin-bottom: 8px; }
3. CSS 说明
- 颜色和边框:浅灰色(#e0e0e0)和圆角边缘用于模拟真实元素。
-
闪光效果:
linear-gradient
使用@keyframes shimmer
创建动画发光效果。 - 真实比例:定义每个元素(图像、标题、文本)的尺寸,以便它们类似于真实内容。
4. 显示真实内容(可选)
要在一段时间后显示实际内容,请将此 JavaScript 代码添加到文件中 script.js
:
setTimeout(() => { document.querySelector('.card').innerHTML = ` <img src="/static/imghwm/default1.png" data-src="https://via.placeholder.com/300x150" class="lazy" alt="Imagen de ejemplo"> <h3 id="Título-del-contenido">Título del contenido</h3> <p>Este es un texto de ejemplo para la tarjeta.</p> `; }, 3000); // Simula una carga de 3 segundos
在 HTML 的 .card
元素中,根据要显示为加载程序的每个元素添加 .skeleton
、.skeleton.image
、.skeleton.title
和 .skeleton.text
类。
?结论
骨架加载器是改善用户体验的简单而有效的方法,给您的应用程序带来速度和专业的印象。 在您的项目中尝试一下!
以上是如何使用 CSS 创建骨架加载器并改善应用程序的用户体验的详细内容。更多信息请关注PHP中文网其他相关文章!

选择Flexbox还是Grid取决于布局需求:1)Flexbox适用于一维布局,如导航栏;2)Grid适合二维布局,如杂志式布局。两者在项目中可结合使用,提升布局效果。

包含CSS文件的最佳方法是使用标签在HTML的部分引入外部CSS文件。1.使用标签引入外部CSS文件,如。2.对于小型调整,可以使用内联CSS,但应谨慎使用。3.大型项目可使用CSS预处理器如Sass或Less,通过@import导入其他CSS文件。4.为了性能,应合并CSS文件并使用CDN,同时使用工具如CSSNano进行压缩。

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

重构自己的代码看起来是什么样的?约翰·瑞亚(John Rhea)挑选了他写的一个旧的CSS动画,并介绍了优化它的思维过程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingmatematingmultationmatingMultationPropertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用BombingingWithjavofofofofofoffo

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他们可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑战挑战InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)创造性

使用滚动阴影,尤其是对于移动设备,是克里斯以前涵盖的一个微妙的UX。杰夫(Geoff)涵盖了一种使用动画限制属性的新方法。这是另一种方式。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版
中文版,非常好用

SublimeText3 英文版
推荐:为Win版本,支持代码提示!