


A simple and crude explanation of the transform attribute in css3_html/css_WEB-ITnose
Please note in advance that the scope of this article is still limited to 2D transform.
I really love the transform attribute of CSS3. With this feature, various special effects can be easily achieved. To quote a lyric, "Spinning, jumping, I won't stop." Transform is so fashionable and willful. Of course, his willfulness is not only in terms of function, but also in terms of use. Let’s take a look at the introduction to the use of transform 2D on the W3C official website.
There are a lot of them, so let’s classify them first: one matrix, three translate, three scale, three skew, and one rotate. The three translates are divided into three types: XY, X, and Y. I drew a picture to roughly show the relationship between these operations.
Seeing this, some people may wonder why matrix is at the top of all other operations. In fact, other operations except matrix are an extension of matrix. What does it mean? ? The operations of translate, scale, skew and rotate are all used by people who do not understand the matrix principle. I am afraid that you do not understand the principle, so I have encapsulated several methods to allow you to use the matrix method conveniently. How to prove it? Interested children can check the style of the element (note: not the class rule, but the calculated style), or output the transform attribute of the element. See if you get uniform matrix results, similar to:
matrix(1, 0.466307658154999, 0, 1, 0, 0)
No more verbosity, since this is an operation, it all ends up matrix, so what is the relationship between them? If I use the knowledge of matrices to tell you how to calculate, it will not fit the title of this article, and there are already many such articles, so I will not show my shame and stick to the principle of the title: simple and crude explanation.
First let’s talk about translate, which means “transfer”. First, explain the syntax of translate:
transform:translate(25px,26px)
, which means shifting 25 pixels and 26 pixels to the right and down. Then we convert it to matrix syntax and look at this line of statement:
transform: matrix(1, 0, 0, 1, 25, 26);Sharp-eyed children’s shoes should have discovered the problem. The last 25 and 26 represent the displacement of the X-axis and Y-axis. Children’s shoes who are interested can try the X-displacement and Y-displacement by themselves, here I won’t use it to take up space. Of course, the initial value of transform must be emphasized here. That is, the default value of matrix when there is no transformation is: transform: matrix(1, 0, 0, 1, 0, 0);, to avoid You can’t understand the explanation below.
Then there is scale. I am used to translating it into "zoom", so the translation also corresponds to its function: scaling component. Demonstrate the syntax:
transform:scale(1.1,1.2);Similarly, let’s take a look at the statement converted into matrix format:
transform: matrix(1.1, 0, 0, 1.2, 0, 0);Obviously, the scaling of X here is The scaling of the Y-axis corresponds to the first value and the fourth value in the matrix respectively. Their values correspond to how many times they are magnified. Still the same, separate tests for the X-axis and Y-axis will not be done.
Let’s take a look at skew again. I translate it as “tilt”. The function of skew is to tilt the component at a certain angle. It can be used to draw parallelograms and resemble the “inertial motion” we have seen before. " is achieved through this. It is more reliable to use a single Skew to demonstrate
transform: skewX(45deg);transform: skewY(45deg);The corresponding statement in matrix format is:
transform: matrix(1, 0, 1, 1, 0, 0); transform: matrix(1, 1, 0, 1, 0, 0);Did you find the problem? The X-direction tilt corresponds to changing the third value, and the Y-direction tilt corresponds to changing the second value. Then let me explain the meaning of this value. This value is the tan value of the angle that needs to be deflected, and it needs to be rotated at an angle of 45°. , corresponding to 1. If you are interested, you can try other values to verify.
At this point, we understand what the six values of the matrix represent:
The first element: X-axis magnification
The second Element: The tan value of the Y-axis tilt angle
The third element: The tan value of the X-axis tilt angle
The fourth element: Remarks for Y-axis amplification
Five elements: the pixel size shifted to the right
The sixth element: the pixel size shifted downward.
It’s not over yet. There is another ratio that has not been explained. Before explaining it, let’s take a look at an illustration:
These two pictures look like Is the effect almost the same? They are indeed almost the same, but the difference is that the above picture is realized through rotate, while the picture below is realized through skew, which is rotated at an angle of 45 degrees. Take a look at the two generated styles respectively:
transform: matrix(0.707106781186548, 0.707106781186548, -0.707106781186548, 0.707106781186548, 0, 0); transform: matrix(1, 0.466307658154999, 0.466307658154999, 1, 0, 0);Seeing this, many people have questions. What are these decimal points? The 0.7 above refers to one-half of the square root, which is cos45° or sin45°. So what exactly are these two calculation methods? This has to go back to the principle of matrix implementation to explain. Let me briefly explain the principle of transform: first find a center point (the default is the middle), and then calculate the coordinates of each pixel relative to that point (the upper left corner of 100*100 That is -50,50), then get the six values passed in by the matrix and 0, 0, 1 to form a new matrix, and then perform an operation with the matrix composed of the original coordinates to get a new matrix, and then add it to the new matrix Get the new X coordinate and the new Y coordinate.
Well, it’s no longer simple to say it like this. It’s a bit deviated from my title. Let’s explain it simply and crudely. The above matrix is replaced by: (a,b,c,d,e,f ) six values, then calculate the relative coordinates of each pixel based on the center point, and then calculate the new coordinates. The calculation formula for the new coordinates is:
x' = a * x c * y e; y' = b * x d * y f; When rotating, the values passed in are cosθ, sinθ, sinθ and cosθ respectively, and the values passed in by skew are: cosθ/consθ, sinθ/cosθ, sinθ/cosθ, cosθ/cosθ. So this is why the image is enlarged by a factor of 2 when using skew for rotation.
In fact, under normal circumstances, it is enough to use the matrix formula using the six calculation methods mentioned above. This last part of this article is only written for the completeness of the article, but it has lost the principle of simplicity and crudeness. .

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment