Interpretation of CSS 3D view properties: transform and perspective

WBOY
Release: 2023-10-24 08:11:09
Original
1037 people have browsed it

CSS 3D 视图属性解读:transform 和 perspective

Interpretation of CSS 3D view properties: transform and perspective, specific code examples are required

Introduction:
In modern web design, 3D effects have become a very important Popular elements. Through the transform and perspective properties of CSS, we can easily add 3D visual effects to web pages to make them more vivid and attractive. This article will explain these two properties and provide specific code examples.

1. Transform attribute:
The transform attribute is a very powerful attribute in CSS. It can realize various transformation operations such as rotation, scaling, movement and tilt of elements. In the 3D view, we can use the transform attribute to transform elements in 3D space.

The following are some commonly used transform functions:

  1. Rotation:
    transform: rotateX(45deg); // Rotate 45 degrees around the X axis
    transform: rotateY( 45deg); // Rotate 45 degrees around the Y axis
    transform: rotateZ(45deg); // Rotate 45 degrees around the Z axis
  2. Scale:
    transform: scale(2); // In Magnify twice on the X and Y axes
    transform: scaleX(2); // Magnify twice on the X axis
    transform: scaleY(2); // Magnify twice on the Y axis
  3. Move:
    transform: translateX(100px); // Move 100 pixels on the X axis
    transform: translateY(100px); // Move 100 pixels on the Y axis
    transform: translateZ(100px ); // Move 100 pixels on the Z axis
  4. Tilt:
    transform: skewX(45deg); // Tilt 45 degrees along the X axis
    transform: skewY(45deg); // Along The Y-axis is tilted 45 degrees

By combining different transform functions, we can achieve complex 3D transformation effects. For example, we can use rotation, scaling, and movement to create a 3D cube:

Front
Back
Left
Right
Top
Bottom
Copy after login
.cube {
  width: 200px;
  height: 200px;
  position: relative;
  transform-style: preserve-3d;
}

.face {
  position: absolute;
  width: 200px;
  height: 200px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
}

.front {
  transform: translateZ(100px);
}

.back {
  transform: translateZ(-100px) rotateY(180deg);
}

.left {
  transform: translateX(-100px) rotateY(-90deg);
}

.right {
  transform: translateX(100px) rotateY(90deg);
}

.top {
  transform: translateY(-100px) rotateX(90deg);
}

.bottom {
  transform: translateY(100px) rotateX(-90deg);
}
Copy after login

This code will create a 200x200 pixel cube with a translucent black background color on each side. The transformation of each face in 3D space can be achieved through the transform attribute. For example, by giving one of the faces translateZ(100px), you can move it forward 100 pixels relative to the viewpoint, thus forming the front face of the cube.

2. Perspective attribute:
The perspective attribute is used to set the perspective effect of the element. By setting the position and observation distance of the perspective point, it can affect the performance of the 3D transformation effect. Perspective also needs to be used with the transform attribute.

You can set the perspective attribute to make elements produce far and near effects in 3D space. For example:

.cube {
  width: 200px;
  height: 200px;
  position: relative;
  transform: rotateX(45deg) rotateY(45deg);
  transform-style: preserve-3d;
  perspective: 1000px;
}
Copy after login

In this example, by setting perspective: 1000px, we give the element a far and near effect of 1000 pixels. You can adjust the distance of an element in 3D space by changing the perspective value.

Summary:
By using the transform and perspective properties of CSS, we can easily add 3D effects to web pages. The transform attribute can implement transformation operations such as rotation, scaling, movement, and tilt of elements in 3D space. The perspective attribute can control the perspective effect of elements in 3D space. By combining the two, we are able to create a rich variety of 3D effects.

Reference:

  • CSS Transform (https://developer.mozilla.org/zh-CN/docs/Web/CSS/transform)
  • CSS Perspective (https://developer.mozilla.org/zh-CN/docs/Web/CSS/perspective)

(Note: The above example code is only for demonstration effect, actual application needs to be based on specific needs Make adjustments)

The above is the detailed content of Interpretation of CSS 3D view properties: transform and perspective. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!