HTML5 CSS3 attractive example: 3D cube rotation Animation

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/lmj623565791/article/details/34120047

Creative from: http://www.html5tricks.com/demo/html5-3d-cube/index.html, students send me an example, I feel very good, but really don't want to come out of the practical use, but the effect is very dazzling ~

:


Knowledge point:

1. perspective and transform Review

2. css3 backgroud implements the lattice background, that is, the small lattice ON THE SURFACE

3. @-webkit-keyframes implement animation


HTML:

<body><div class="stage">    <div class="cube">        <div class="font"></div>        <div class="back"></div>        <div class="left"></div>        <div class="right"></div>        <div class="top"></div>        <div class="bottom"></div>    </div></div></body>

In the previous 3D product presentation, we have already talked about how to create a cube, and there are numbers on it. In theory, it is more complicated than this one, although the wood has this cool ~ I will not talk about it here.

CSS:

 html        {            background: -webkit-radial-gradient(center, ellipse, #430d6d 0%, #000000 100%);            background: radial-gradient(ellipse at center, #430d6d 0%, #000000 100%);            height: 100%;        }        .stage        {            -webkit-perspective: 1000px;            width: 20em;            height: 20em;            left: 50%;            top: 50%;            margin-left: -10em;            margin-top: -10em;            position: absolute;        }        .cube        {            position: absolute;            width: 100%;            height: 100%;            -webkit-transform-style: preserve-3d;            -webkit-transform: rotateX(-20deg) rotateY(-20deg);        }        .cube *        {            background: -webkit-linear-gradient(left, rgba(54, 226, 248, 0.5) 0px, rgba(54, 226, 248, 0.5) 3px, rgba(0, 0, 0, 0) 0px), -webkit-linear-gradient(top, rgba(54, 226, 248, 0.5) 0px, rgba(54, 226, 248, 0.5) 3px, rgba(0, 0, 0, 0) 0px);            -webkit-background-size: 2.5em 2.5em;            background-color: rgba(0, 0, 0, 0.5);            position: absolute;            width: 100%;            height: 100%;            border: 2px solid rgba(54, 226, 248, 0.5);            -webkit-box-shadow: 0 0 5em rgba(0, 128, 0, 0.4);        }        .font        {            -webkit-transform: translateZ(10em);        }        .back        {            -webkit-transform: rotateX(180deg) translateZ(10em);        }        .left        {            -webkit-transform: rotateY(-90deg) translateZ(10em);        }        .right        {            -webkit-transform: rotateY(90deg) translateZ(10em);        }        .top        {            -webkit-transform: rotateX(90deg) translateZ(10em);        }        .bottom        {            -webkit-transform: rotateX(-90deg) translateZ(10em);        }

Similarly: as a stage, the cube sets the effect of sub-elements to 3d, and then rotates and sets translateZ on each side to form a cube.

Set the backgroud code for each side:

     background: -webkit-linear-gradient(                left,                rgba(54, 226, 248, 0.5) 0px,                rgba(54, 226, 248, 0.5) 3px,                rgba(0, 0, 0, 0) 0px),                        -webkit-linear-gradient(                top,                rgba(54, 226, 248, 0.5) 0px,                rgba(54, 226, 248, 0.5) 3px,                rgba(0, 0, 0, 0) 0px);            -webkit-background-size: 2.5em 2.5em;

Set the background to 3-pixel lines from left to right, 3-pixel lines from top to bottom, set the background size to 2.5em 2.5em, and repeat the background, the effect is as follows (I added a border ):



Current complete results:


We can see that the cube has been formed, and the animation can be added at last. Do not think that the animation is very complicated, but it is actually very simple ~

Define an animation frame:

 @-webkit-keyframes spin        {            from            {                -webkit-transform: translateZ(-10em) rotateX(0) rotateY(0deg);                transform: translateZ(-10em) rotateX(0) rotateY(0deg);            }            to            {                -webkit-transform: translateZ(-10em) rotateX(360deg) rotateY(360deg);                transform: translateZ(-10em) rotateX(360deg) rotateY(360deg);            }        }

The name is spin. At the beginning, translateZ (-10em) rotateX (0) rotateY (0deg); At the end: translateZ (-10em) rotateX (360deg) rotateY (360deg ); that is, it rotates around x and Y axes at 360 degrees simultaneously.

Add the animation attribute to the Cube:

  .cube        {            -webkit-animation: 6s spin linear infinite;            position: absolute;            width: 100%;            height: 100%;            -webkit-transform-style: preserve-3d;            -webkit-transform: rotateX(-20deg) rotateY(-20deg);        }

Set the animation time to 6 s, animation spin, speed to uniform linear, infinite loop infinite;

For more detailed parameter settings, refer to w3cSchool ~ I will also write a blog about the attributes of CSS3 in the future ~



Well, the final result is complete ~

The appearance of the original website is slightly different:

Because it adds a radial gradient to each side, we add the following:

    .cube *:before        {            display: block;            background: -webkit-radial-gradient(center, ellipse, rgba(0, 0, 0, 0) 30%, rgba(0, 128, 0, 0.2) 100%);            background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0) 30%, rgba(0, 128, 0, 0.2) 100%);            content: '';            height: 100%;            width: 100%;            position: absolute;        }

Use the pseudo element before and then set the radial gradient ~~ Now it is consistent ~








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.