Understanding the CSS transform-origin property

Understanding the CSS transform-origin property

Preface

I recently made a fireworks animation, which is an animation of fireworks dispersing. During the implementation of the animation, I got stuck mainly during the rotation of the fireworks. Later I found that it was mainly because I didn’t understand transform-origin property well enough. I specially found an example to practice and deepened my understanding of the property.

transform-origin effect

This property is used to change the origin of the element's deformation and is most commonly used in conjunction with rotation. The number of received parameters can be one, two or three. When it is two values, they represent the values ​​from the left side of the box model, such as transform-origin: 50px 50px; which means that the rotation center of the container becomes the upper left corner of the box model as the origin, and the X and Y axes rotate with a distance of 50px from the origin.

Drawing of the hour hand of a clock

The vertical bar in the middle is our initial setting, and the following ones are rotated based on it

  <div class="clock">
    <div class="hour"></div>
    <div class="hour"></div>
    <div class="hour"></div>
    <div class="hour"></div>
    <div class="hour"></div>
  </div>

As can be seen from the CSS code below, we set the rotation center to the (3,105)px of the first vertical line as the origin for rotation. The distance here is the value from the left side of the box model. Understanding this, you can write other hour hands, and then rotate them separately to get the hour hands. Because I didn't understand the relative position of the value here for calculation, I stepped on a lot of pitfalls.

CSS

.hour {
  position: absolute;
  left: 105px;
  width: 6px;
  height: 50px;
  background-color: #000;
  border-radius:6px;
  -webkit-transform-origin:3px 105px; 
          transform-origin:3px 105px;
}
.hour:nth-child(2) {
  transform:rotate(45deg);
}
.hour:nth-child(3) {
  transform:rotate(90deg);
}
.hour:nth-child(4) {
  transform:rotate(-45deg);
}
.hour:nth-child(5) {
  transform:rotate(-90deg);
}

refer to

MDN

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Vue uses vue meta info to set the title and meta information of each page

>>:  Example code for implementing auto-increment sequence in mysql

Recommend

Linux ssh server configuration code example

Use the following terminal command to install the...

How to generate Vue user interface by dragging and dropping

Table of contents Preface 1. Technical Principle ...

js to achieve image fade-in and fade-out effect

This article shares the specific code of js to ac...

WebWorker encapsulates JavaScript sandbox details

Table of contents 1. Scenario 2. Implement IJavaS...

Detailed explanation of the role of the new operator in Js

Preface Js is the most commonly used code manipul...

React implements a general skeleton screen component example

Table of contents What is a skeleton screen? Demo...

MySQL database operations and data types

Table of contents 1. Database Operation 1.1 Displ...

Using CSS3 and JavaScript to develop web color picker example code

The web color picker function in this example use...

Introduction to the use of common Dockerfile commands

Table of contents 01 CMD 02 ENTRYPOINT 03 WORKDIR...

HTML imitates Baidu Encyclopedia navigation drop-down menu function

HTML imitates the Baidu Encyclopedia navigation d...

Detailed explanation of Linux copy and paste in VMware virtual machine

1. Linux under VMware Workstation: 1. Update sour...

Vue project packaging and optimization implementation steps

Table of contents Packaging, launching and optimi...

Web skills: Multiple IE versions coexistence solution IETester

My recommendation Solution for coexistence of mul...