当前位置: 首页 > news >正文

如何用纯 CSS 创作一个货车 loader

1446586357-5b70ceb1d54c1_articlex.gif

效果预览

在线演示

按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。

https://codepen.io/comehope/pen/vaPGRz

可交互视频

此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

请用 chrome, safari, edge 打开观看。

https://scrimba.com/p/pEgDAM/cz32DUd

源代码下载

本地下载

每日前端实战系列的全部源代码请从 github 下载:

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器代表卡车,包含的 2 个子元素代表车头和尾气;<hr> 代表道路:

&lt;div class="truck"&gt;
    &lt;span class="cab"&gt;&lt;/span&gt;
    &lt;span class="smoke"&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;hr&gt;

居中显示,同时道路与页面之间留出空间:

body {
    margin: 10%;
    padding-top: 10%;
}

画出卡车车厢:

.truck {
    width: 15em;
    height: 5em;
    font-size: 10px;
    background-color: #444;
    border-radius: 0.4em;
}

用伪元素画出车厢的车轮:

.truck {
    position: relative;
}

.truck::before,
.truck::after {
    content: '';
    position: absolute;
    box-sizing: border-box;
    width: 2em;
    height: 2em;
    background-color: #444;
    border: 0.1em solid white;
    border-radius: 50%;
    bottom: -1em;
}

.truck::before {
    left: 0.6em;
}

.truck::after {
    right: 0.6em;
}

画出车头:

.cab {
    position: absolute;
    width: 3.3em;
    height: 2.5em;
    background-color: #333;
    left: -3.5em;
    bottom: 0;
    border-radius: 40% 0 0.4em 0.4em;
}

.cab::before {
    content: '';
    position: absolute;
    width: 2em;
    height: 1.5em;
    background-color: #333;
    top: -1.5em;
    right: 0;
    border-radius: 100% 0 0 0;
}

画出车头的车轮:

.cab::after {
    content: '';
    position: absolute;
    box-sizing: border-box;
    width: 2em;
    height: 2em;
    background-color: #444;
    border: 0.1em solid white;
    border-radius: 50%;
    bottom: -1em;
    left: 0.5em;
}

画出尾气的初始状态:

.smoke,
.smoke::before,
.smoke::after {
    content: '';
    position: absolute;
    width: 1em;
    height: 1em;
    background-color: #333;
    right: -0.1em;
    bottom: -0.5em;
    border-radius: 50%;
}

增加排出尾气的动画:

.smoke {
    animation: smoke-1 2s infinite;
}

.smoke::before {
    animation: smoke-2 2s infinite;
}

.smoke::after {
    animation: smoke-3 2s infinite;
}

@keyframes smoke-1 {
    to {
        width: 3em;
        height: 3em;
        right: -3em;
        bottom: 0.5em;
    }
}

@keyframes smoke-2 {
    to {
        width: 2.5em;
        height: 2.5em;
        right: -6em;
        bottom: 0.8em;
    }
}

@keyframes smoke-3 {
    to {
        width: 3.5em;
        height: 3.5em;
        right: -4em;
        bottom: 0.2em;
    }
}

增加尾气的飘散效果:

.smoke {
    animation:
        drift 2s infinite,
        smoke-1 2s infinite;
}

.smoke::before {
    animation: 
        drift 3s infinite,
        smoke-2 3s infinite;
}

.smoke::after {
    animation: 
        drift 4s infinite,
        smoke-3 4s infinite;
}

@keyframes drift {
    0%, 100% {
        filter: opacity(0);
    }

    15% {
        filter: opacity(0.9);
    }
}

增加卡车行驶的动画效果:

.truck {
    animation: 
        move 5s infinite;
}

@keyframes move {
    0% {
        margin-left: 90%;
    }

    50% {
        margin-left: 45%;
    }

    100% {
        margin-left: 0;
    }

    0%, 100% {
        filter: opacity(0);
    }

    10%, 90% {
        filter: opacity(1);
    }
}

增加卡片行驶中颠簸的动画效果:

.truck {
    animation: 
        put-put 2s infinite,
        move 10s infinite;
}

@keyframes put-put {
    0% {
        margin-top: 0;
        height: 5em;
    }

    5% {
        margin-top: -0.2em;
        height: 5.2em;
    }

    20% {
        margin-top: -0.1em;
        height: 5em;
    }

    35% {
        margin-top: 0.1em;
        height: 4.9em;
    }

    40% {
        margin-top: -0.1em;
        height: 5.1em;
    }

    60% {
        margin-top: 0.1em;
        height: 4.9em;
    }

    75% {
        margin-top: 0;
        height: 5em;
    }

    80% {
        margin-top: -0.4em;
        height: 5.2em;
    }

    100% {
        margin-top: 0.1em;
        height: 4.9em;
    }
}

大功告成!

原文地址:https://segmentfault.com/a/1190000015982054

相关文章:

  • 阿里云马劲:保证云产品持续拥有稳定性的实践和思考
  • C# 获取对象 大小 Marshal.SizeOf (sizeof 只能在不安全的上下文中使用)
  • Oracle-SQL*Plus 简单操作
  • thinkphp 使用paginate分页搜索带参数
  • Money去哪了- 每日站立会议
  • ethereumjs/merkle-patricia-tree-2-API
  • 腾讯音乐赴美IPO仅11亿美元,疑受科技股抛售和中美贸易战影响
  • 【quick-cocos2d-lua】 基本类及用法
  • mysql安装时无法启动服务解决方案
  • Linux初级运维(十五)——bash脚本编程之函数
  • Hystrix断路器在微服务网关中的应用
  • 怎么把工作型PPT制作的好看又专业?
  • git切换分支
  • 数据加密:RSA 加解密
  • react 前端项目技术选型、开发工具、周边生态
  • Apache Spark Streaming 使用实例
  • github从入门到放弃(1)
  • JAVA并发编程--1.基础概念
  • Laravel Mix运行时关于es2015报错解决方案
  • node学习系列之简单文件上传
  • Octave 入门
  • Python_OOP
  • ReactNative开发常用的三方模块
  • React系列之 Redux 架构模式
  • vue-router 实现分析
  • 如何用vue打造一个移动端音乐播放器
  • 智能合约开发环境搭建及Hello World合约
  • 自动记录MySQL慢查询快照脚本
  • MPAndroidChart 教程:Y轴 YAxis
  • ​【已解决】npm install​卡主不动的情况
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (3)(3.2) MAVLink2数据包签名(安全)
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (ZT)北大教授朱青生给学生的一封信:大学,更是一个科学的保证
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (二)pulsar安装在独立的docker中,python测试
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (十一)图像的罗伯特梯度锐化
  • (四)docker:为mysql和java jar运行环境创建同一网络,容器互联
  • (算法)求1到1亿间的质数或素数
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • (转)Linq学习笔记
  • ***测试-HTTP方法
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .net Signalr 使用笔记
  • .NET 使用配置文件
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET使用存储过程实现对数据库的增删改查
  • .Net中的集合