0 开始,但我希望它从 TOP 到 BOTTOM 执-6ren">
gpt4 book ai didi

javascript - 变小时更改元素 "moves towards"的方向

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:06 25 4
gpt4 key购买 nike

首先,我为这个可怕的标题道歉,但这可能是我无法自己找到答案的原因。

我正在练习一些 CSS 动画,我希望 form 元素的高度从 100px -> 0 开始,但我希望它从 TOP 到 BOTTOM 执行此操作.相反,默认情况下,它从底部到顶部。

那么我该如何更改元素决定在其自身上“关闭”的位置呢?

const form   = document.querySelector('form');
const submit = document.querySelector('.submit');
const div = document.querySelector('div');

submit.addEventListener('click', e => {
e.preventDefault();

form.classList.add('height');
/* div.classList.add('div--animate'); */
});
form {
/* background: lightblue; */
bottom: 0;
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100px;
overflow: hidden;
position: relative;
transition: height .15s linear, width .15s linear;
width: 400px;
}

.height {
height: 0;
width: 0;
}
<form>
<input type="text" placeholder="name">
<input type="text" placeholder="email">
<input class="submit" type="submit">
</form>

最佳答案

您可以考虑缩放变换并调整变换原点来决定您的元素应该放在哪里。如果需要,您还可以保留宽度/高度,但为它们添加延迟,以便它们在缩放结束时发生变化。

const form   = document.querySelector('form');
const submit = document.querySelector('.submit');
const div = document.querySelector('div');

submit.addEventListener('click', e => {
e.preventDefault();

form.classList.add('height');
});
form {
bottom: 0;
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100px;
overflow: hidden;
position: relative;
transition: transform .15s linear,height 0s .15s linear,width 0s .15s linear;
transform-origin:bottom right; /* center | top right | left | ...*/
width: 400px;
}

.height {
width:0;
height:0;
transform:scale(0);
}
<form>
<input type="text" placeholder="name">
<input type="text" placeholder="email">
<input class="submit" type="submit">
</form>

关于javascript - 变小时更改元素 "moves towards"的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49712789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com