gpt4 book ai didi

css - 按钮 CSS 过渡/动画悬停问题

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:30 24 4
gpt4 key购买 nike

我想创建一个悬停时会发生变化的按钮。您可以在这里查看:https://drive.google.com/file/d/1fIcil2Xyky8DC2NRcfZBKXCMHk9gpat2/view?usp=sharing .

在这里你可以看到我的尝试:https://codepen.io/koravski/pen/BEvKRP

body {
display: flex;
justify-content: center;
align-items: center;
background: #333;
font-family: 'Lato', sans-serif;
}

.txt {
position: absolute;
color: #fff;
}

.box {
width: 200px;
height: 50px;
background-color: red;
transition: all 1s;
}

.box:hover {
width: 50px;
height: 5px;
margin-top: 40px;
}
<div class="txt">Call to action</div>
<div class="box"></div>

问题是当我悬停它时,它开始循环播放。它应该在红色矩形上,这样它就不会循环。

如果您有任何建议,那就太好了。谢谢。

最佳答案

不太清楚你想要什么效果,但你正在改变大小,所以当它缩小时你不再徘徊在它上面。

我建议改用 transform(实际上是两个,一个用于大小,另一个用于位置)。

html,
body {
height: 100%;
}

body {
display: flex;
justify-content: center;
align-items: center;
background: #333;
font-family: 'Lato', sans-serif;
}

.txt {
position: absolute;
color: #fff;
}

.box {
width: 200px;
height: 50px;
background-color: red;
transition: all 1s;
}

.box:hover {
transform: translateY(40px) scale(.1);
}
<div class="txt">Call to action</div>
<div class="box"></div>

还有其他问题,但这取决于它实际应该做什么。

事实上,您可以使用一个 div 来做到这一点。

html,
body {
height: 100%;
}

body {
display: flex;
justify-content: center;
align-items: center;
background: #333;
font-family: 'Lato', sans-serif;
}

.txt {
position: relative;
color: #fff;
width: 200px;
height: 50px;
line-height: 50px;
text-align: center;
transition: all 1s;
}

.txt:before {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: red;
z-index: -1;
transition: all 1s;
}

.txt:hover:before {
transform: translateY(40px) scale(.1);
}
<div class="txt">Call to action</div>

关于css - 按钮 CSS 过渡/动画悬停问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853590/

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