Home  >  Article  >  Web Front-end  >  CSS3 3D small demo that everyone can understand

CSS3 3D small demo that everyone can understand

高洛峰
高洛峰Original
2017-02-18 15:06:241089browse

css3 3d case summary

I recently got into Web animation, so I will record my learning process and share it with everyone. Let me share with you what I have done recently

1. Rotating puzzle

First look at the effect


人人都能读懂的css3 3d小demo

The code mainly consists of HTML and CSS3, which should be said to be relatively simple.

The first is the HTML code:

  <p class="camera" id="camera">
    <p class="kuai">
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
    </p>

    <p class="kuai">
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
    </p>

    <p class="kuai">
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
    </p>

    <p class="kuai">
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
    </p>

    <p class="kuai">
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
      <p class="mian"></p>
    </p>
  </p>

A 3-layer p HTML framework is used here, and the level is very clear.
The next focus is on the CSS code, how to use the features of CSS3 to achieve 3D and scrolling effects, so that it always looks 3D.

body{  background: red;}/*对于相机元素*/.camera{  perspective: 800px;/*这个属性是视距(景深),就是目测的距离*/
  width: 500px;  height: 300px;  margin: auto;  transform-style: preserve-3d;/*该属性规定如何在 3D 空间中呈现被嵌套的元素。preserve-3d子元素将保留其 3D 位置。*/}/*对于模块元素*/.kuai{  width: 100px;  height: 300px;  margin: 50px auto;  float: left;  transform-style: preserve-3d;  position: relative;  transition:transform 1s;  margin-left: -1px;}/*对于模块的每个面元素*/.kuai:nth-of-type(2)>.mian{  background-position: -100px 0;}.kuai:nth-of-type(3)>.mian{  background-position: -200px 0;}.kuai:nth-of-type(4)>.mian{  background-position: -300px 0;}.kuai:nth-of-type(5)>.mian{  background-position: -400px 0;}.kuai:nth-of-type(6)>.mian{  background-position: -500px 0;}.mian{  width: 100px;  height: 300px;  position: absolute;  font-size:100px;  text-align: center;  line-height: 300px;  transform-style: preserve-3d;  background-color: black;  backface-visibility: hidden;}.mian:nth-of-type(1){  transform: translateZ(150px);  background:url(../images/pic1.jpg) no-repeat;  background-size: 500px 100%;}.mian:nth-of-type(2){  transform: translateZ(-150px) rotateX(180deg);  background:url(../images/pic2.png) no-repeat;  background-size: 500px 100%;}.mian:nth-of-type(3){  width: 300px;  transform: rotateY(-90deg) translateZ(150px);/*3d中transform属性是关键的,它主要是靠旋转 移动 缩放来变形实现的*/}.mian:nth-of-type(4){  width: 300px;  transform: rotateY(90deg) translateZ(-50px);}.mian:nth-of-type(5){  transform: rotateX(90deg) translateZ(150px);  background:url(../images/pic3.jpg) no-repeat;  background-size: 500px 100%;}.mian:nth-of-type(6){  transform: rotateX(-90deg) translateZ(150px);  background:url(../images/pic4.jpg) no-repeat;  background-size: 500px 100%;}

The last thing is js

window.onload=function() {  var camera=document.getElementById('camera');  var kuais=camera.getElementsByClassName('kuai');  var chishu=0;  var geshu=5;
  camera.onclick=function() {
    chishu++;    for(var i=0;i<geshu;i++){
      kuais[i].style="transition:transform 1s "+100*i+"ms;transform:rotateX("+chishu*90+"deg)";
    }
  }
}

There is an attribute here that is not used for transform-origin. It is used to change the origin position of the element and does not need to be set. As shown in the figure
人人都能读懂的css3 3d小demo
Similarly, it can also be set to: transform-origin:top right (the upper right corner is the origin), transform-origin:bottom right (the lower right corner is the origin), transform-origin:bottom left (the lower left corner is the origin)

Summary

In short, in the process of learning CSS3, I saw a lot of new features and learned how to use them, but what I want to say is No matter how we learn how to use it, we also need to understand why each line of code produces the corresponding effect. Especially for 3D transform, we need to fundamentally understand the 3D space in order to better grasp what each of its attribute values ​​can bring. come effect.

For more articles related to css3 3d small demo that everyone can understand, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn