纯CSS3实现的8种炫酷Loading动画效果解析

2 下载量 102 浏览量 更新于2024-09-05 收藏 65KB PDF 举报
“纯CSS3实现的8种Loading动画效果” 本文将详细介绍如何使用纯CSS3技术来创建8种不同的Loading动画效果。这些效果设计简洁、酷炫,符合现代前端开发的审美趋势,对于想要在网站或应用中添加吸引人的加载指示器的开发者来说,是一个非常有价值的参考。 首先,所有这8种效果的基础HTML结构都是相同的,如下所示: ```html <div class="loader">加载中</div> ``` 一个简单的`<div>`元素,包含了“加载中”的文本,并且赋予了类名`.loader`,这个类名将在CSS中定义具体的动画效果。 接下来,我们将逐一解析每种Loading动画的CSS代码。 1. 第一种效果的CSS代码如下: ```css .load1.loader, .load1.loader:before, .load1.loader:after { background: #FFF; -webkit-animation: load1 1s infinite ease-in-out; animation: load1 1s infinite ease-in-out; width: 1em; height: 4em; } .load1.loader:before, .load1.loader:after { position: absolute; top: 0; content: ''; } .load1.loader:before { left: -1.5em; } .load1.loader { text-indent: -9999em; margin: 40% auto; position: relative; font-size: 11px; -webkit-animation-delay: 0.16s; animation-delay: 0.16s; } .load1.loader:after { left: 1.5em; -webkit-animation-delay: 0.32s; animation-delay: 0.32s; } @-webkit-keyframes load1 { 0%, 80%, 100% { box-shadow: 0 0 #FFF; height: 4em; } 40% { box-shadow: 0 -2em #ffffff; height: 5em; } } @keyframes load1 { 0%, 80%, 100% { box-shadow: 0 0 #FFF; height: 4em; } 40% { box-shadow: 0 -2em #ffffff; height: 5em; } } ``` 这种效果通过`@keyframes`定义了一个名为`load1`的动画,改变`box-shadow`和`height`属性,使得元素在不同时间点呈现不同的外观,从而形成动画效果。 2. 第二种效果的CSS代码: 由于提供的文本不完整,无法给出第二种效果的具体CSS代码。通常,它会包含与第一种效果类似的结构,但会使用不同的动画名称、时间和样式,以创建独特的Loading动画。 其余6种效果的CSS代码将遵循类似的原则,通过调整`@keyframes`内的样式变化,以及可能使用伪元素(`:before`和`:after`)来增强视觉效果。这些效果可能会涉及到旋转、缩放、颜色变换等多种CSS3特性,例如`transform`、`animation-duration`、`animation-timing-function`等。 纯CSS3实现的Loading动画利用了CSS3的动画功能,如关键帧动画(`@keyframes`),以及过渡和变换(`transition`和`transform`),这些都无需JavaScript即可创建出动态且吸引人的界面元素。开发者可以根据自己的需求,结合这些例子进行调整和创新,为自己的项目增添更多个性化和交互性的元素。
2014-12-31 上传
总共分为三层:一层为圆形边线,一层为进度边线,一层用来显示标识进度节点。 public class CircleProgressBar extends View { private int maxProgress = 100; private int progress = 15; private int progressStrokeWidth = 2; private int marxArcStorkeWidth = 16; // 画圆所在的距形区域 RectF oval; Paint paint; public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub oval = new RectF(); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { // TODO 自动生成的方法存根 super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); width = (width > height) ? height : width; height = (width > height) ? height : width; paint.setAntiAlias(true); // 设置画笔为抗锯齿 paint.setColor(Color.WHITE); // 设置画笔颜色 canvas.drawColor(Color.TRANSPARENT); // 白色背景 paint.setStrokeWidth(progressStrokeWidth); // 线宽 paint.setStyle(Style.STROKE); oval.left = marxArcStorkeWidth / 2; // 左上角x oval.top = marxArcStorkeWidth / 2; // 左上角y oval.right = width - marxArcStorkeWidth / 2; // 左下角x oval.bottom = height - marxArcStorkeWidth / 2; // 右下角y canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景 paint.setColor(Color.rgb(0x57, 0x87, 0xb6)); paint.setStrokeWidth(marxArcStorkeWidth); canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 绘制进度圆弧,这里是蓝色 paint.setStrokeWidth(1); String text = progress + "%"; int textHeight = height / 4; paint.setTextSize(textHeight); int textWidth = (int) paint.measureText(text, 0, text.length()); paint.setStyle(Style.FILL); canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, paint); } public int getMaxProgress() { return maxProgress; } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } /** * 设置进度 * * @param progress * 进度百分比 * @param view * 标识进度的节点视图 */ public void setProgress(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.invalidate(); } /** * 非UI线程调用 */ public void setProgressNotInUiThread(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.postInvalidate(); } /** * 进度标注点的动画 * * @param fromDegrees * @param toDegrees * @return */ private Animation pointRotationAnima(float fromDegrees, float toDegrees) { int initDegress = 306;// 进度点起始位置(图片偏移约54度) RotateAnimation animation = new RotateAnimation(fromDegrees, initDegress + toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(1);// 设置动画执行时间 animation.setRepeatCount(1);// 设置重复执行次数 animation.setFillAfter(true);// 设置动画结束后是否停留在结束位置 return animation; } }