我一直在努力擴展我的 CSS 知識,并做了一個簡單的 3d 轉換演示,展示了一組在平面上以 3d 旋轉的正方形。
我最初使用旋轉角度的@property 定義來撰寫它,后來意識到我可以使用簡單的 var 定義來完成它。但是,當我更改它時,應該保持一致距離的正方形在影片周期中開始變平。我不明白為什么會這樣,我希望有人能指出我正確的方向。
在這里它正在作業。請注意,Firefox 不處理 @property 函式(因此需要更改它)
顯示代碼片段
:root{
--cubeSize: 32vw;
}
* { margin: 0; padding: 0;}
body, html{
height: 100%;
width: 100%;
overflow: hidden;
}
body{
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
* {
perspective: 70vw;
transform-style: preserve-3d;
}
#boxContainer {
display: flex;
justify-content: center;
align-items: center;
width: 0;
height: 0;
position: relative;
transform: translateZ(-50vw);
}
#boxContainer > *:nth-child(1){
background-color: #f00;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -16vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(2){
background-color: #f40;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -12vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(3){
background-color: #ff0;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -8vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(4){
background-color: #8f0;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -4vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(5){
background-color: #0f0;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 0;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(6){
background-color: #0f8;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 4vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(7){
background-color: #0ff;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 8vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(8){
background-color: #66f;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 12vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(9){
background-color: #f0f;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 16vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
@property --animAngY {
syntax : '<angle>';
initial-value: 0deg;
inherits: false;
}
@property --animAngX {
syntax : '<angle>';
initial-value: 0deg;
inherits: false;
}
@property --animAngZ {
syntax : '<angle>';
initial-value: 0deg;
inherits: false;
}
#boxContainer > * {
--gridColour: #0005;
--spacing: 5mm;
background-image:
repeating-linear-gradient(90deg, var(--gridColour) 0px, var(--gridColour) 1px, #fff0 0px, #fff0 var(--spacing)),
repeating-linear-gradient(0deg, var(--gridColour) 0px, var(--gridColour) 1px, #fff0 0px, #fff0 var(--spacing))
;
position: absolute;
opacity: 1;
display: inline-block;
width: var(--cubeSize);
height: var(--cubeSize);
display: inline-block;
transform-origin: 50% 50%;
transform:
rotateX(
calc(var(--Xangle) var(--animAngX))
)
rotateY(
calc(var(--Yangle) var(--animAngY))
)
rotateZ(
calc(var(--Zangle) var(--animAngZ))
)
translateX(
var(--Xoffset)
)
translateY(
var(--Yoffset)
)
translateZ(
var(--Zoffset)
)
;
animation-name: cubeSpin;
animation-duration: 10s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
@keyframes cubeSpin {
to {
--animAngX : 1080deg;
--animAngY : 360deg;
--animAngZ : 720deg;
}
}
#floor{
--gridColour : #8af;
--bgColour: #0000;
--spacing: 10vw;
--zPos: -10vw;
--lineWidth: 0.25vw;
position: absolute;
display: inline-block;
width: 500vw;
height: 500vw;
background-color: #000;
border-radius: 50%;
background-image:
repeating-linear-gradient(
90deg,
var(--gridColour) 0vw,
var(--gridColour) var(--lineWidth),
var(--bgColour) var(--lineWidth),
var(--bgColour) var(--spacing)
),
repeating-linear-gradient(
0deg,
var(--gridColour) 0vw,
var(--gridColour) var(--lineWidth),
var(--bgColour) var(--lineWidth),
var(--bgColour) var(--spacing)
)
;
transform: rotateX(90deg) rotateZ(0deg) translateZ(var(--zPos));
animation-name: floorSpin;
animation-duration: 10s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
@keyframes floorSpin {
to {
transform: rotateX(90deg) rotateZ(360deg) translateZ(var(--zPos));
}
}
body{
background-image: linear-gradient( black 0, #1a0450 25%, #1e8ad9 56% );
}
<body>
<div id="floor"></div>
<div id="boxContainer">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
如果我更換這部分:
@keyframes cubeSpin {
to {
--animAngX : 1080deg;
--animAngY : 360deg;
--animAngZ : 720deg;
}
}
有了這個:
@keyframes cubeSpin {
to {
transform:
rotateX(
calc(var(--Xangle) 1080deg)
)
rotateY(
calc(var(--Yangle) 360deg)
)
rotateZ(
calc(var(--Zangle) 720deg)
)
}
}
那么這就是結果
顯示代碼片段
:root{
--cubeSize: 32vw;
}
* { margin: 0; padding: 0;}
body, html{
height: 100%;
width: 100%;
overflow: hidden;
}
body{
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
* {
perspective: 70vw;
transform-style: preserve-3d;
}
#boxContainer {
display: flex;
justify-content: center;
align-items: center;
width: 0;
height: 0;
position: relative;
transform: translateZ(-50vw);
}
#boxContainer > *:nth-child(1){
background-color: #f00;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -16vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(2){
background-color: #f40;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -12vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(3){
background-color: #ff0;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -8vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(4){
background-color: #8f0;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -4vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(5){
background-color: #0f0;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 0;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(6){
background-color: #0f8;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 4vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(7){
background-color: #0ff;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 8vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(8){
background-color: #66f;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 12vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(9){
background-color: #f0f;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 16vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
@property --animAngY {
syntax : '<angle>';
initial-value: 0deg;
inherits: false;
}
@property --animAngX {
syntax : '<angle>';
initial-value: 0deg;
inherits: false;
}
@property --animAngZ {
syntax : '<angle>';
initial-value: 0deg;
inherits: false;
}
#boxContainer > * {
--gridColour: #0005;
--spacing: 5mm;
background-image:
repeating-linear-gradient(90deg, var(--gridColour) 0px, var(--gridColour) 1px, #fff0 0px, #fff0 var(--spacing)),
repeating-linear-gradient(0deg, var(--gridColour) 0px, var(--gridColour) 1px, #fff0 0px, #fff0 var(--spacing))
;
position: absolute;
opacity: 1;
display: inline-block;
width: var(--cubeSize);
height: var(--cubeSize);
display: inline-block;
transform-origin: 50% 50%;
transform:
rotateX(
calc(var(--Xangle) var(--animAngX))
)
rotateY(
calc(var(--Yangle) var(--animAngY))
)
rotateZ(
calc(var(--Zangle) var(--animAngZ))
)
translateX(
var(--Xoffset)
)
translateY(
var(--Yoffset)
)
translateZ(
var(--Zoffset)
)
;
animation-name: cubeSpin;
animation-duration: 10s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
@keyframes cubeSpin {
to {
transform:
rotateX(
calc(var(--Xangle) 1080deg)
)
rotateY(
calc(var(--Yangle) 360deg)
)
rotateZ(
calc(var(--Zangle) 720deg)
)
}
}
#floor{
--gridColour : #8af;
--bgColour: #0000;
--spacing: 10vw;
--zPos: -10vw;
--lineWidth: 0.25vw;
position: absolute;
display: inline-block;
width: 500vw;
height: 500vw;
background-color: #000;
border-radius: 50%;
background-image:
repeating-linear-gradient(
90deg,
var(--gridColour) 0vw,
var(--gridColour) var(--lineWidth),
var(--bgColour) var(--lineWidth),
var(--bgColour) var(--spacing)
),
repeating-linear-gradient(
0deg,
var(--gridColour) 0vw,
var(--gridColour) var(--lineWidth),
var(--bgColour) var(--lineWidth),
var(--bgColour) var(--spacing)
)
;
transform: rotateX(90deg) rotateZ(0deg) translateZ(var(--zPos));
animation-name: floorSpin;
animation-duration: 10s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
@keyframes floorSpin {
to {
transform: rotateX(90deg) rotateZ(360deg) translateZ(var(--zPos));
}
}
body{
background-image: linear-gradient( black 0, #1a0450 25%, #1e8ad9 56% );
}
<body>
<div id="floor"></div>
<div id="boxContainer">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
正如您在這些演示中看到的那樣,第一個按我的預期作業,而在第二個中,旋轉平面在整個影片中一起折疊
編輯:
擴展一下它的作用:
底部網格只是一個旋轉的 div,它作業正常,并且確實在影片中使用了更廣泛支持的 CSS 更改。
螢屏中間是一個沒有寬度和高度的 div,它包含了 9 個 div 作為旋轉層
圖層本身正在旋轉,而不是父 div - 如果我理解正確的話 - 旋轉父 div 會將這些子級渲染為投影到父級平面上。
所有圖層都由相同的影片“cubeSpin”進行轉換,因為它與 div #boxContainer 的所有子級相關聯。
uj5u.com熱心網友回復:
您缺少轉換的翻譯部分
transform:
rotateX(
calc(var(--Xangle) 1080deg)
)
rotateY(
calc(var(--Yangle) 360deg)
)
rotateZ(
calc(var(--Zangle) 720deg)
)
translateX(
var(--Xoffset)
)
translateY(
var(--Yoffset)
)
translateZ(
var(--Zoffset)
)
顯示代碼片段
:root{
--cubeSize: 32vw;
}
* { margin: 0; padding: 0;}
body, html{
height: 100%;
width: 100%;
overflow: hidden;
}
body{
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
* {
perspective: 70vw;
transform-style: preserve-3d;
}
#boxContainer {
display: flex;
justify-content: center;
align-items: center;
width: 0;
height: 0;
position: relative;
transform: translateZ(-50vw);
}
#boxContainer > *:nth-child(1){
background-color: #f00;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -16vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(2){
background-color: #f40;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -12vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(3){
background-color: #ff0;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -8vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(4){
background-color: #8f0;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : -4vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(5){
background-color: #0f0;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 0;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(6){
background-color: #0f8;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 4vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(7){
background-color: #0ff;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 8vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(8){
background-color: #66f;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 12vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
#boxContainer > *:nth-child(9){
background-color: #f0f;
--Xoffset : 0cm;
--Yoffset : 0cm;
--Zoffset : 16vw;
--Xangle : 0deg;
--Yangle : 90deg;
--Zangle : 0deg;
}
@property --animAngY {
syntax : '<angle>';
initial-value: 0deg;
inherits: false;
}
@property --animAngX {
syntax : '<angle>';
initial-value: 0deg;
inherits: false;
}
@property --animAngZ {
syntax : '<angle>';
initial-value: 0deg;
inherits: false;
}
#boxContainer > * {
--gridColour: #0005;
--spacing: 5mm;
background-image:
repeating-linear-gradient(90deg, var(--gridColour) 0px, var(--gridColour) 1px, #fff0 0px, #fff0 var(--spacing)),
repeating-linear-gradient(0deg, var(--gridColour) 0px, var(--gridColour) 1px, #fff0 0px, #fff0 var(--spacing))
;
position: absolute;
opacity: 1;
display: inline-block;
width: var(--cubeSize);
height: var(--cubeSize);
display: inline-block;
transform-origin: 50% 50%;
transform:
rotateX(
calc(var(--Xangle) var(--animAngX))
)
rotateY(
calc(var(--Yangle) var(--animAngY))
)
rotateZ(
calc(var(--Zangle) var(--animAngZ))
)
translateX(
var(--Xoffset)
)
translateY(
var(--Yoffset)
)
translateZ(
var(--Zoffset)
)
;
animation-name: cubeSpin;
animation-duration: 10s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
@keyframes cubeSpin {
to {
transform:
rotateX(
calc(var(--Xangle) 1080deg)
)
rotateY(
calc(var(--Yangle) 360deg)
)
rotateZ(
calc(var(--Zangle) 720deg)
)
translateX(
var(--Xoffset)
)
translateY(
var(--Yoffset)
)
translateZ(
var(--Zoffset)
)
}
}
#floor{
--gridColour : #8af;
--bgColour: #0000;
--spacing: 10vw;
--zPos: -10vw;
--lineWidth: 0.25vw;
position: absolute;
display: inline-block;
width: 500vw;
height: 500vw;
background-color: #000;
border-radius: 50%;
background-image:
repeating-linear-gradient(
90deg,
var(--gridColour) 0vw,
var(--gridColour) var(--lineWidth),
var(--bgColour) var(--lineWidth),
var(--bgColour) var(--spacing)
),
repeating-linear-gradient(
0deg,
var(--gridColour) 0vw,
var(--gridColour) var(--lineWidth),
var(--bgColour) var(--lineWidth),
var(--bgColour) var(--spacing)
)
;
transform: rotateX(90deg) rotateZ(0deg) translateZ(var(--zPos));
animation-name: floorSpin;
animation-duration: 10s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
@keyframes floorSpin {
to {
transform: rotateX(90deg) rotateZ(360deg) translateZ(var(--zPos));
}
}
body{
background-image: linear-gradient( black 0, #1a0450 25%, #1e8ad9 56% );
}
<body>
<div id="floor"></div>
<div id="boxContainer">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/530741.html
標籤:cssCSS动画css 变量
下一篇:在div的中心對齊文本和圖示