CSS3 实现童年的纸飞机


Posted in HTML / CSS onMay 05, 2019

今天我们来折纸飞机(可以飞出去的那种哦)

基本全用css来实现,只有一小部分的js

首先看一下飞机的构造

CSS3 实现童年的纸飞机

灰色区域为可折叠区域

白色区域为机身

三角形由border画出来的再经过各种平移翻转变成上图

写之前再补充个知识点:

我们颜色的设置不用rgba,

用hsl() h: 色调 0- 360 0(或360)表示红色,120表示绿色,240表示蓝色

 s : 饱和度 0% -100%

 l : 亮度 0% - 100%

先看效果才有动力:

CSS3 实现童年的纸飞机

HTML:

<!--童年的纸飞机-->
<div class="airplane">
    <div class="front-end show-front">
        <!--宽高自适应的文本框-->
        <div class="text-input" contenteditable = true></div>
        <div class="fly">
            fly
        </div>
    </div>
    <div class="backup-end show-backup">
        <div class="left-plane">
            <!--左上角折叠区域-->
            <div class="left-top fold"></div>
            <!--左下角折叠区域-->
            <div class="left-bottom fold"></div>
            <!--机身-->
            <div class="wing wing1"></div>
            <div class="wing wing2"></div>
        </div>
        <div class="right-plane">
            <!--右上角折叠区域-->
            <div class="right-top fold"></div>
            <!--右下角折叠区域-->
            <div class="right-bottom fold"></div>
            <!--机身-->
            <div class="wing wing3"></div>
            <div class="wing wing4"></div>
        </div>
    </div>
</div>

css:

body{
    width: 100%;
    height: 680px;
    background-color: #000;
    background-repeat: no-repeat;
    overflow: hidden;
    transition: all 2s linear;
}
/*景深加在父级上*/
.airplane{
    width: 100%;
    height: 100%;
    -webkit-perspective: 800px;
    -webkit-perspective-origin: 50% 50%;
}
/*纸飞机前面*/
/*一开始不旋转*/
.front-end.show-front{
    transform: rotateY(0deg);
}
/*点击后旋转*/
.front-end{
    background: rgba(255, 255, 255, 0.15);
    *background: hsl(0, 0%, 88%);
    /*绕Y轴旋转-180度*/
    transform: rotateY(-180deg);
    position: relative;
    box-sizing: border-box;
    padding: 20px;
    text-align: center;
    backface-visibility: hidden;
    width: 400px;
    height: 260px;
    top: 240px;
    transition: all 0.8s ease-in-out;
    margin: auto;
}
/*文本框*/
.text-input{
    width: 100%;
    max-width:360px;
    min-height:100px;
    padding: 10px;
    box-sizing: border-box;
    height: 140px;
    background-color: #ffffff;
    font-smoothing: subpixel-antialiased;
    font-size: 18px;
    text-align: left;
    font-family: "Microsoft YaHei",Helvetica, Arial, Verdana;
    line-height: 20px;
}
.fly{
    transition: all 0.3s ease-in-out;
    /*hsl是色调/饱和度/亮度/*/
    border: 2px solid hsl(194, 100%, 72%);
    margin: 15px 0;
    padding: 10px;
    outline: none;
    font-size: 18px;
    cursor: pointer;
    font-family: "Microsoft YaHei";
    background-color: hsl(0, 0%, 94%);
    border-radius:4px;
    user-select: none;
}
/*点击按钮时缩小动画*/
.fly:active{
    transform: scale(0.85);
    transition: all 10ms ease-in-out;
    background-color: hsl(0, 0%, 85%);
    border: 2px solid hsl(194, 30%, 55%);
}
.backup-end{
    perspective: 600px;
    perspective-origin: 200px 131px;
    transform-style: preserve-3d;
    transition: all 0.8s ease-in-out;
    backface-visibility: hidden;
    position: relative;
    width: 400px;
    height: 260px;
    margin: auto;
}
/*一开始不显示飞机*/
.backup-end.show-backup{
    transform: rotateY(180deg);
}
/*飞机的左右两边公共样式*/
.left-plane, .right-plane{
    transform-style: preserve-3d;
    width: 200px;
    height: 260px;
    display: block;
    position: absolute;
    top: 0px;
    transition: all 1s ease-in-out;
}
/*左边*/
.left-plane{
    transform: rotateZ(0deg);
    transform-origin: 100% 50% 0;
    left: 0;
}
/*右边*/
.right-plane{
    transform: rotateZ(0deg);
    transform-origin: 0% 50%;
    left: 199px;
}
/*左右机身的公共样式*/
.wing{
    position: absolute;
    transform-origin: 0 0 0;
    perspective: 1px;
    perspective-origin: 50% 50%;
    backface-visibility: hidden;
    transition: all 1.3s linear;
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    background: none;
    border: none;
    border-top: 240px solid hsla(0, 0%, 0%, 0);
    border-bottom: 0px solid hsla(0, 0%, 0%, 0);
    border-right: 100px solid hsl(0, 0%, 88%);
    width: 0;
    height: 0;
    bottom: 0;
}
/*绘制  飞机2d 雏形*/
.wing1 {
    transform-origin: 100% 100%;
    transform: translateY(-38px) translateX(8px) rotateZ(22.62deg) skewY(-22.62deg);/*2D图像的偏移 旋转*/
}

.wing2 {
    transform: rotateZ(22.62deg);
    transform-origin: 100% 100%;
    border-left: 100px solid hsl(0, 0%, 88%);
    border-right: none;
    left: 100px;
}

.wing3 {
    transform: rotateZ(-22.62deg);
    transform-origin: 0% 100%;
    border-right: 100px solid hsl(0, 0%, 88%);
}

.wing4 {
    transform: translateY(-38px) translateX(-8px) rotateZ(-22.62deg) skewY(22.62deg);
    transform-origin: 0% 100%;
    border-right: none;
    border-left: 100px solid hsl(0, 0%, 88%);
    left: 100px;
}
/*绘制可折叠区域*/
.left-top.fold{
    position: absolute;
    transform-origin: 100px 112px;
    transition-delay: 1300ms;
    width: 0;
    height: 0;
    top: 0;
    border-right: 202px solid hsla(0, 0%, 0%, 0);
    border-bottom: 202px solid hsla(0, 0%, 0%, 0);
    border-top: 222px solid hsl(0, 0%, 88%);
}
.right-top.fold{
    position: absolute;
    right: 0;
    border-left: 202px solid hsla(0, 0%, 0%, 0);
    border-bottom: 202px solid hsla(0, 0%, 0%, 0);
    border-top: 222px solid hsl(0, 0%, 88%);
    transform-origin: 96px 112px;
    transition-delay: 1650ms;
}
.left-bottom.fold{
    position: absolute;
    transform-origin: 109px 0;
    transition-delay: 2100ms;
    width: 109px;
    height: 38px;
    background: hsl(0, 0%, 88%);
    bottom: 0;
    left: 0;
}
.right-bottom.fold{
    position: absolute;
    transform-origin: 0 0;
    transition-delay: 2450ms;
    width: 109px;
    height: 38px;
    background: hsl(0, 0%, 88%);
    bottom: 0;
    right: 0;
}
/*补全 折叠尾翼 剩余 三角区域*/
.left-bottom.fold:after {
    position: absolute;
    content: "";
    border-right: 92px solid hsla(0, 0%, 0%, 0);
    border-bottom: 39px solid hsl(0, 0%, 88%);
    border-top: 37px solid hsla(0, 0%, 0%, 0);
    left: 109px;
    bottom: 0;
}

.right-bottom.fold:after {
    position: absolute;
    content: "";
    border-left: 92px solid hsla(0, 0%, 0%, 0);
    border-bottom: 39px solid hsl(0, 0%, 88%);
    border-top: 37px solid hsla(0, 0%, 0%, 0);
    left: -92px;
    bottom: 0;
}

/****************************/
/****此处开始配合js*****/
/*折叠效果*/
.fold {
    transition: transform 800ms ease-out;
    backface-visibility: hidden;
    position: absolute;
    background-color: transparent;
    z-index: 0;
    width: 0;
}
/* 折叠效果(左机翼、左尾翼) */
.left-top.fold.curved {
    transform: rotate3d(1,-1.11,0,180deg);
}

.left-bottom.fold.curved {
    transform: rotate3d(2.4867,1,0,-180deg);
}
/* 折叠效果(右机翼、右尾翼)*/
.right-top.fold.curved {
    transform: rotate3d(1,1.11,0,180deg);
}

.right-bottom.fold.curved {
    transform: rotate3d(-2.4867,1,0,180deg);
}

/* 平放一整个飞机 */
.airplane.hover {
    transform: rotateX(54deg) rotateY(-10deg) rotateZ(25deg);
    transition-delay: 0.5s;
}
/*放平之后 左侧整体倾斜 (体现折叠效果)*/
.backup-end.hover .left-plane {
    transform: rotateY(60deg);
}

.backup-end.hover .right-plane {
    transform: rotateY(-60deg);
}
/* 3d视觉中放平 左侧机翼*/
.backup-end.hover .wing1 {
    transform: translateY(-38px) translateX(8px) rotateZ(22.62deg) rotateY(-60deg) skewY(-22.62deg);
    border-right: 100px solid hsl(0, 0%, 95%);
}
/*左侧 飞机手持部位透明度降低*/
.backup-end.hover .wing2 {
    border-left: 100px solid hsl(0, 0%, 85%);
}

/* 3d视觉中放平 右侧机翼*/
.backup-end.hover .wing4 {
    transform: translateY(-38px) translateX(-8px) rotateZ(-22.62deg) rotateY(60deg) skewY(20deg);
    border-left: 100px solid hsl(0, 0%, 95%);
}

/*右侧 飞机手持部位透明度降低*/
.backup-end.hover .wing3 {
    border-right: 100px solid hsl(0, 0%, 71%);
}

/*机翼 折叠效果(右机翼、右尾翼) 之后 多余部分隐藏掉*/
.backup-end.hover .curved {
    display: none;
}

/* #wind_container.hover .wing {
    backface-visibility: visible;
} */


/* 飞机后退助跑 */
.backup-end.hover.fly_away_first {
    transform: translateX(-100px) translateZ(300px) rotateX(42deg) rotateY(-11deg) rotateZ(27deg);
    transition-delay: 0ms;
    transition-duration: 0.4s;
    transition-timing-function: ease-out;
}
/* 飞机向前飞翔至消失 */
.backup-end.hover.fly_away_first.fly_away {
    transform: translateX(600px) translateY(-400px) translateZ(-5000px) rotateX(66deg) rotateY(-12deg) rotateZ(36deg);
    transition: transform 2s ease-out, opacity 1.5s 0.5s linear;
    opacity: 0;
}

js:

// 童年的纸飞机
const fly = document.getElementsByClassName('fly')[0];
const front = document.getElementsByClassName('front-end')[0];
const backup = document.getElementsByClassName('backup-end')[0];
const fold = document.getElementsByClassName('fold');
fly.addEventListener('click', () => {
    first().then(second).then(third).then(fourth).then(fifth).catch((err)=> {
        console.log(err)
    });
}, false);
// 第一步
function first() {
    return new Promise((suc, err) => {
        setTimeout(() => {
            // 隐藏信息面板
            front.classList.remove('show-front');
            // 翻转至正面
            backup.classList.remove('show-backup');
            // 折叠效果(左翼、右翼)
            for (let i = 0; i < fold.length; i++) {
                fold[i].classList.add('curved')
            }
            // 颜色变换
            document.body.style.backgroundColor = "#54575A";
            suc(1)
        }, 200)
    })
}
function second() {
    return new Promise((suc, err) => {
        setTimeout(function () {
            backup.classList.add('hover');
            document.body.style.backgroundColor = "#AD8BD8";
            suc(2)
        }, 2800);
    })
}
//步骤三:飞机后退助跑
function third() {
    return new Promise((suc, err) => {
        setTimeout(function () {
            backup.classList.add('fly_away_first');
            document.body.style.backgroundColor = "#6E99C4";
            suc(3)
        }, 2000);
    })
}
// 步骤四:飞机向前飞翔至消失
function fourth() {
    return new Promise((suc, err) => {
        setTimeout(function () {
            backup.classList.add('fly_away');
            document.body.style.backgroundColor = "#3F9BFF";
            suc(4)
        }, 600);
    })
}
function fifth() {
    return new Promise((suc, err) => {
        setTimeout(function () {
            front.classList.add('show-front');
            backup.classList.remove('fly_away','fly_away_first','hover');
            backup.classList.add('show-backup');
            for (let i = 0; i < fold.length; i++) {
                fold[i].classList.remove('curved')
            }
            document.body.style.backgroundColor = "#000";
            suc(5)
        }, 3000);
    })
}

总结

以上所述是小编给大家介绍的CSS3 实现童年的纸飞机 ,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

HTML / CSS 相关文章推荐
CSS3实现跳动的动画效果
Sep 12 HTML / CSS
利用CSS3实现自定义滚动条代码分享
Aug 18 HTML / CSS
CSS3截取字符串实例代码【推荐】
Jun 07 HTML / CSS
非常震撼的纯CSS3人物行走动画
Feb 24 HTML / CSS
CSS3实现千变万化的文字阴影text-shadow效果设计
Apr 26 HTML / CSS
利用css3径向渐变做一张优惠券的示例
Mar 22 HTML / CSS
html5实现完美兼容各大浏览器的播放器
Dec 26 HTML / CSS
JavaScript+Canvas实现自定义画板的示例代码
May 13 HTML / CSS
HTML5实现移动端弹幕动画效果
Aug 01 HTML / CSS
使用html2canvas实现将html内容写入到canvas中生成图片
Jan 03 HTML / CSS
在CSS中映射鼠标位置并实现通过鼠标移动控制页面元素效果(实例代码)
Apr 22 HTML / CSS
使用 CSS 轻松实现一些高频出现的奇形怪状按钮
Dec 06 HTML / CSS
使用CSS变量实现炫酷惊人的悬浮效果
Apr 26 #HTML / CSS
详解CSS3中的box-sizing(content-box与border-box)
Apr 19 #HTML / CSS
CSS3 transforms应用于背景图像的解决方法
Apr 16 #HTML / CSS
CSS3实现王者荣耀匹配人员加载页面的方法
Apr 16 #HTML / CSS
CSS3实现王者匹配时的粒子动画效果
Apr 12 #HTML / CSS
CSS3解析抖音LOGO制作的方法步骤
Apr 11 #HTML / CSS
浅谈CSS3 box-sizing 属性 有趣的盒模型
Apr 02 #HTML / CSS
You might like
PHP 缓存实现代码及详细注释
2010/05/16 PHP
php set_magic_quotes_runtime() 函数过时解决方法
2010/07/08 PHP
变量在 PHP7 内部的实现(一)
2015/12/21 PHP
PHP实现获取第一个中文首字母并进行排序的方法
2017/05/09 PHP
javascript针对DOM的应用分析(四)
2012/04/15 Javascript
js获取location.href的参数实例代码
2013/08/02 Javascript
Iframe 自动适应页面的高度示例代码
2014/02/26 Javascript
jQuery+CSS实现的网页二级下滑菜单效果
2015/08/25 Javascript
jQuery实现的简单折叠菜单(折叠面板)效果代码
2015/09/16 Javascript
ajax 提交数据到后台jsp页面及页面跳转问题
2017/01/19 Javascript
JavaScript的for循环中嵌套一个点击事件的问题解决
2017/03/03 Javascript
Node.js使用Koa搭建 基础项目
2018/01/08 Javascript
Vue2.0 实现歌手列表滚动及右侧快速入口功能
2018/08/08 Javascript
简述vue路由打开一个新的窗口的方法
2018/11/29 Javascript
详解vantUI框架在vue项目中的应用踩坑
2018/12/06 Javascript
node.js基于socket.io快速实现一个实时通讯应用
2019/04/23 Javascript
如何使用Javascript中的this关键字
2020/05/28 Javascript
[00:09]DOTA2新版本PA至宝特效动作展示
2014/11/19 DOTA
[03:19]2016国际邀请赛中国区预选赛第四日TOP10镜头集锦
2016/07/01 DOTA
零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
2014/11/05 Python
Python安装Numpy和matplotlib的方法(推荐)
2017/11/02 Python
django项目搭建与Session使用详解
2018/10/10 Python
python文件拆分与重组实例
2018/12/10 Python
python按照多个条件排序的方法
2019/02/08 Python
Python3中_(下划线)和__(双下划线)的用途和区别
2019/04/26 Python
Python Django框架实现应用添加logging日志操作示例
2019/05/17 Python
在Tensorflow中实现leakyRelu操作详解(高效)
2020/06/30 Python
5款实用的python 工具推荐
2020/10/13 Python
Python3.8.2安装包及安装教程图文详解(附安装包)
2020/11/28 Python
英国第一的购买便宜玩具和游戏的在线购物网站:Bargain Max
2018/01/24 全球购物
暑期社会实践心得体会
2014/09/02 职场文书
老干部工作汇报材料
2014/10/28 职场文书
城管年度个人总结
2015/02/28 职场文书
中标通知书范本
2015/04/17 职场文书
小学生班干部竞选稿
2015/11/20 职场文书
团队合作精神学习心得体会
2016/01/19 职场文书