Axe*_*xel 13 css sass css3 parallax css-transforms
translateZ使用set perspective(关键字:"parallax")相对于父级宽度/高度的父容器内部计算子元素的宽度/高度的公式是什么?我想在两个轴上创建一个具有视差效果的站点.除了一件事,我能够弄清楚我的模型需要的一切.当它超过100%时,如何计算儿童的宽度/高度.由于父母的视角和儿童的翻译,儿童的宽度/高度在视觉上不再与父母的宽度/高度对齐.
用于缩放子元素的公式是:1 + (translateZ * -1) / perspective.但我无法找到宽度/高度的公式.顺便说一句:当孩子的宽度/高度<= 100%时,一切正常.
但是当宽度> = 100%时,请查看下图中的结果(容器具有顶部偏移以使事物可见).
为了正确,在我的特定情况下,方法是让所有子元素在视觉上具有相同的宽度/高度.
在SASS(首选):CSS中的PEN或SassMeister
:PEN
来自可能有用的规范的链接:
https://www.w3.org/TR/css-transforms-1/#recomposing-to-a-3d-matrix
https://www.w3.org/TR/css-变换-1 /#数学-描述
"谷歌搜索"很多,但没有找到任何指向我正确方向的东西.提前致谢...
html, body {
height: 100%;
overflow: hidden;
width: 100%;
}
#projection {
perspective: 1px;
perspective-origin: 0 0;
height: 100%;
overflow: auto;
width: 100%;
}
.pro {
transform: scale(1) translate(0px, 0px) translateZ(0px);
height: 100%;
position: absolute;
transform-origin: 0 0;
transform-style: preserve-3d;
width: 100%;
}
.pro--1 {
transform: scale(4) translate(0px, 0px) translateZ(-3px);
width: 110%;
}
.pro--2 {
transform: scale(3) translate(0px, 50%) translateZ(-2px);
width: 110%;
}
.pro--3 {
transform: scale(2) translate(0px, 100%) translateZ(-1px);
width: 110%;
}
.pro {
background: #333;
box-shadow: inset 0 0 0 5px orange;
color: orange;
font-size: 4em;
line-height: 1em;
text-align: center;
}
.pro--2 {
background: rgba(75, 75, 75, 0.5);
box-shadow: inset 0 0 0 5px green;
color: green;
line-height: 4em;
}
.pro--3 {
background: rgba(75, 75, 75, 0.5);
box-shadow: inset 0 0 0 5px white;
color: white;
line-height: 7em;
}Run Code Online (Sandbox Code Playgroud)
<div id="projection">
<div class="pro pro--1">pro--1</div>
<div class="pro pro--2">pro--2</div>
<div class="pro pro--3">pro--3</div>
</div>Run Code Online (Sandbox Code Playgroud)
@mixin projection($translateZ: 0, $translateX: 0, $translateY: 0, $width: 0, $height: 0, $perspective: $perspective)
// strip and sanitize units for further calculations
// units must be "px" for both $translateZ and $perspective
$unit: unit( $translateZ )
@if '' != $unit
$translateZ: $translateZ / ($translateZ * 0 + 1)
@if 'px' != $unit
@warn '$translateZ must have "px" as unit!'
$unit: unit( $perspective )
@if '' != $unit
$perspective: $perspective / ($perspective * 0 + 1)
@if 'px' != $unit
@warn '$perspective must have "px" as unit!'
$unit: 0px // yeah - technically this is no unit
// calculate scaling factor
$scale: 1 + ($translateZ * -1) / $perspective
// sanitize units for translateX, translateY, translateZ
$translateZ: $translateZ + $unit
@if unitless( $translateX )
$translateX: $translateX + $unit
@if unitless( $translateY )
$translateY: $translateY + $unit
// render css "transform: scale() translate(x, y) translateZ()"
transform: scale( $scale ) translate($translateX, $translateY) translateZ( $translateZ + $unit )
$width: 110% // 100% works like a charme
$translateZ--1: -3 // "px" will be added in mixin
$translateZ--2: -2
$translateZ--3: -1
$perspective: 1
html, body
height: 100%
overflow: hidden
width: 100%
#projection
perspective: $perspective + 0px
perspective-origin: 0 0
height: 100%
overflow: auto
width: 100%
.pro
@include projection()
height: 100%
position: absolute
transform-origin: 0 0
transform-style: preserve-3d
width: 100%
.pro--1
@include projection( $translateZ--1 )
width: $width
.pro--2
@include projection( $translateZ--2, 0, 50% )
width: $width
.pro--3
@include projection( $translateZ--3, 0, 100% )
width: $width
Run Code Online (Sandbox Code Playgroud)
在设置透视的父容器内使用translateZ 计算子元素的宽度/高度的公式是:
$scale = 1 + (translateZ * -1) / perspective。
在哪里扔这个公式transform: scale( $scale )。
perspective是父母的视角值,translateZ也是元素的translateZ 值。根据需要查看底部的片段或SASS 模型。
所以实际上问题中已经给出了答案!
虽然这是所问问题的正确答案,但这根本没有解决我的具体问题,所以这是一个后续问题。
致谢:@Trolleymusic ,他的回答至少让我大开眼界。
CSS 示例:
/* formular: 1 + (translateZ * -1) / perspective */
#parent {
perspective: 10px;
}
.child--1 { /* 1 + (-100 * -1) / 10 */
transform: translateZ(-100px) scale( 11 );
}
.child--2 { /* 1 + (-200 * -1) / 10 */
transform: translateZ(-200px) scale( 21 );
}
.child--3 { /* 1 + (1 * -1) / 10 */
transform: translateZ(1px) scale( 0.9 );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1825 次 |
| 最近记录: |