CSS3 realizes the childhood paper airplane

CSS3 realizes the childhood paper airplane

Today we are going to make origami airplanes (the kind that can fly)

Basically all of it is implemented with CSS, with only a small part of JS

First, let’s look at the structure of the aircraft.

The gray area is the foldable area

The white area is the fuselage

The triangle is drawn by the border and then translated and flipped in various ways to become the above picture

Before writing, let me add one more point of knowledge:

We don't use rgba for color settings.

Use hsl() h: hue 0-360 0 (or 360) represents red, 120 represents green, 240 represents blue

s : Saturation 0% -100%

l : Brightness 0% - 100%

See the effect first before you have motivation:

HTML:

<!--Paper airplanes of childhood-->
<div class="airplane">
    <div class="front-end show-front">
        <!--Text box with adaptive width and height-->
        <div class="text-input" contenteditable = true></div>
        <div class="fly">
            fly
        </div>
    </div>
    <div class="backup-end show-backup">
        <div class="left-plane">
            <!--Folding area in the upper left corner-->
            <div class="left-top fold"></div>
            <!--Folding area in the lower left corner-->
            <div class="left-bottom fold"></div>
            <!--Body-->
            <div class="wing wing1"></div>
            <div class="wing wing2"></div>
        </div>
        <div class="right-plane">
            <!--Folding area in the upper right corner-->
            <div class="right-top fold"></div>
            <!--Folding area in the lower right corner-->
            <div class="right-bottom fold"></div>
            <!--Body-->
            <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;
}
/*Depth of field added to parent*/
.airplane{
    width: 100%;
    height: 100%;
    -webkit-perspective: 800px;
    -webkit-perspective-origin: 50% 50%;
}
/*Paper airplane front*/
/*Do not rotate at the beginning*/
.front-end.show-front{
    transform: rotateY(0deg);
}
/*Click to rotate*/
.front-end{
    background: rgba(255, 255, 255, 0.15);
    *background: hsl(0, 0%, 88%);
    /*Rotate -180 degrees around the Y axis*/
    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 box*/
.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 is hue/saturation/brightness/*/
    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;
}
/*Shrink animation when clicking the button*/
.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;
}
/*Don't show the plane at first*/
.backup-end.show-backup{
    transform: rotateY(180deg);
}
/*Common styles for the left and right sides of the aircraft*/
.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*/
.left-plane{
    transform: rotateZ(0deg);
    transform-origin: 100% 50% 0;
    left: 0;
}
/*right*/
.right-plane{
    transform: rotateZ(0deg);
    transform-origin: 0% 50%;
    left: 199px;
}
/*Common style for left and right fuselage*/
.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;
}
/*Draw the prototype of the 2D aircraft*/
.wing1 {
    transform-origin: 100% 100%;
    transform: translateY(-38px) translateX(8px) rotateZ(22.62deg) skewY(-22.62deg);/*Offset rotation of 2D image*/
}

.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;
}
/*Draw the foldable area*/
.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;
}
/*Complete the remaining triangle area of ​​the folding tail*/
.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;
}

/********************************/
/****Start to cooperate with js***** here/
/*Folding effect*/
.fold {
    transition: transform 800ms ease-out;
    backface-visibility: hidden;
    position: absolute;
    background-color: transparent;
    z-index: 0;
    width: 0;
}
/* Folding effect (left wing, left tail) */
.left-top.fold.curved {
    transform: rotate3d(1,-1.11,0,180deg);
}

.left-bottom.fold.curved {
    transform: rotate3d(2.4867,1,0,-180deg);
}
/* Folding effect (right wing, right tail) */
.right-top.fold.curved {
    transform: rotate3d(1,1.11,0,180deg);
}

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

/* Lay the entire plane flat*/
.airplane.hover {
    transform: rotateX(54deg) rotateY(-10deg) rotateZ(25deg);
    transition-delay: 0.5s;
}
/*After laying flat, the left side is tilted as a whole (reflecting the folding effect)*/
.backup-end.hover .left-plane {
    transform: rotateY(60deg);
}

.backup-end.hover .right-plane {
    transform: rotateY(-60deg);
}
/* Level the left wing in 3D vision */
.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%);
}
/*The transparency of the left aircraft handheld part is reduced*/
.backup-end.hover .wing2 {
    border-left: 100px solid hsl(0, 0%, 85%);
}

/* Level the right wing in 3D vision */
.backup-end.hover .wing4 {
    transform: translateY(-38px) translateX(-8px) rotateZ(-22.62deg) rotateY(60deg) skewY(20deg);
    border-left: 100px solid hsl(0, 0%, 95%);
}

/*The transparency of the right handheld part of the aircraft is reduced*/
.backup-end.hover .wing3 {
    border-right: 100px solid hsl(0, 0%, 71%);
}

/*Wing folding effect (right wing, right tail) and then the excess parts are hidden*/
.backup-end.hover .curved {
    display: none;
}

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


/* Plane moves backwards*/
.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;
}
/* The plane flies forward until it disappears*/
.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:

// Childhood paper airplane 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);
// First step function first() {
    return new Promise((suc, err) => {
        setTimeout(() => {
            // Hide the information panel front.classList.remove('show-front');
            // Flip to the front backup.classList.remove('show-backup');
            // Folding effect (left wing, right wing)
            for (let i = 0; i < fold.length; i++) {
                fold[i].classList.add('curved')
            }
            // Color transformation 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);
    })
}
//Step 3: The plane moves backwards to start running function third() {
    return new Promise((suc, err) => {
        setTimeout(function () {
            backup.classList.add('fly_away_first');
            document.body.style.backgroundColor = "#6E99C4";
            suc(3)
        }, 2000);
    })
}
// Step 4: The plane flies forward until it disappears 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);
    })
}

Summarize

The above is the CSS3 that I introduced to you to realize the childhood paper airplane. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time!

<<:  Solution to ES memory overflow when starting docker

>>:  Design theory: On the issues of scheme, resources and communication

Recommend

An article to give you a deep understanding of Mysql triggers

Table of contents 1. When inserting or modifying ...

Detailed Example of MySQL InnoDB Locking Mechanism

1. InnoDB locking mechanism The InnoDB storage en...

How to start tomcat using jsvc (run as a normal user)

Introduction to jsvc In production, Tomcat should...

Detailed explanation of incompatible changes of components in vue3

Table of contents Functional Components How to wr...

Solution to the problem of MySQL thread in Opening tables

Problem Description Recently, there was a MySQL5....

How to use JSX in Vue

What is JSX JSX is a syntax extension of Javascri...

Mysql 5.7.19 free installation version encountered pitfalls (collection)

1. Download the 64-bit zip file from the official...

Detailed Analysis of Explain Execution Plan in MySQL

Preface How to write efficient SQL statements is ...

How to forget the password of Jenkins in Linux

1.Jenkins installation steps: https://www.jb51.ne...

Detailed tutorial on installing Docker and nvidia-docker on Ubuntu 16.04

Table of contents Docker Installation Nvidia-dock...

How to use Docker to limit container resources

Problem Peeping In the server, assuming that the ...

Solutions for high traffic websites

First: First, confirm whether the server hardware ...

How to implement HTML Table blank cell completion

When I first taught myself web development, there...

Detailed explanation of the use of MySQL sql_mode

Table of contents Preface sql_mode explained The ...

Detailed examples of float usage in HTML/CSS

1. Basic usage examples of float 1. Let's fir...