当前位置: 首页 > 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 前端项目技术选型、开发工具、周边生态
  • [数据结构]链表的实现在PHP中
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • 0x05 Python数据分析,Anaconda八斩刀
  • 230. Kth Smallest Element in a BST
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • ERLANG 网工修炼笔记 ---- UDP
  • ES6 学习笔记(一)let,const和解构赋值
  • HTTP 简介
  • js中的正则表达式入门
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • nodejs:开发并发布一个nodejs包
  • yii2中session跨域名的问题
  • 高度不固定时垂直居中
  • 近期前端发展计划
  • 类orAPI - 收藏集 - 掘金
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 十年未变!安全,谁之责?(下)
  • 收藏好这篇,别再只说“数据劫持”了
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • #Linux(make工具和makefile文件以及makefile语法)
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (定时器/计数器)中断系统(详解与使用)
  • (二)斐波那契Fabonacci函数
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (力扣)循环队列的实现与详解(C语言)
  • (篇九)MySQL常用内置函数
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • (转)shell中括号的特殊用法 linux if多条件判断
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .net core 6 redis操作类
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例