【发布时间】:2021-10-01 11:27:24
【问题描述】:
如果我同时更改width、height 和transform 属性,它会产生非常奇怪的过渡。示例如下。查看矩形及其相对于红线的位置。
我知道将transform 属性替换为left 和top 属性可以解决此问题,但这不是我的解决方案,因为它会产生性能和其他问题。
更新:scale 转换不是解决方案,因为在实际情况下可以更改比例并且内部内容不应受到比例转换的影响。 ——
const states = [{
height: '250px',
width: '500px',
transform: 'translate(50px, 50px)'
},
{
height: '150px',
width: '250px',
transform: 'translate(175px, 100px)'
},
]
let currentState = 0;
function updateState() {
const box = document.querySelector(".box");
const state = states[currentState];
currentState = (currentState + 1) % states.length
box.style.height = state.height
box.style.width = state.width
box.style.transform = state.transform
}
.button {
position: absolute;
top: 0;
left: 0;
}
.box {
transition: 0.4s ease-in-out;
position: absolute;
background: #AAA;
left: 0;
top: 0;
}
.line {
position: absolute;
left: 300px;
height: 100%;
background: red;
width: 1px;
}
.small-box {
width: 10px;
height: 10px;
border: solid 1px black;
position: absolute;
transform: translateX(-50%);
}
<div class="box">
<div class="small-box" style="left: 50%; top:0"></div>
</div>
<div class="line"></div>
<button class="button" onclick="updateState()">Update State</button>
【问题讨论】:
-
您的demo 中没有发生奇怪的
transition。Preview。您能否提供重现此行为的步骤? -
我可以通过多次切换状态来重现它。从一个渲染到下一个渲染似乎不一致。有时它看起来很好,有时它看起来像问题中的动画 gif。
-
@nitrovatter 如果可能的话,我会重新考虑转换
left和top值而不是transform: translate()如果变换“摆动”是不可接受的,但您仍然需要为某些原因。或者,看看您是否可以使@ЖнецЪ 的方法起作用,这可能是我解决问题的方式。
标签: html css css-transitions css-transforms