html - 按钮悬停效果:从左侧滑入,从左侧滑出
问题描述
我想在按钮上创建这种悬停效果:
悬停时,背景颜色会从左侧滑入。如果鼠标离开按钮,则应该返回原始背景颜色,但从左侧滑入。
现在我有这个代码(两个类都适用于按钮):
.button_slide {
color: #FFF;
border: 2px solid rgb(216, 2, 134);
border-radius: 0px;
padding: 18px 36px;
display: inline-block;
font-family: "Lucida Console", Monaco, monospace;
font-size: 14px;
letter-spacing: 1px;
cursor: pointer;
box-shadow: inset 0 0 0 0 #D80286;
-webkit-transition: ease-out 0.4s;
-moz-transition: ease-out 0.4s;
transition: ease-out 0.4s;
}
.slide_right:hover {
box-shadow: inset 400px 0 0 0 #D80286;
}
离开按钮时,我希望悬停背景从左侧消失。现在它从右侧灭亡。
有人可以帮我吗?
解决方案
你试过使用@keyframes
吗?下面有一个例子。
我添加了 0.01px 的传播,否则动画会闪烁。我将尺寸更改为 em,因此可以毫无问题地更改尺寸。
下次请包含您的 HTML!
.button_slide {
border: 2px solid rgb(216, 2, 134);
padding: 1.28571em 2.57143em;
display: inline-block;
font-family: "Lucida Console", Monaco, monospace;
font-size: 20px;
letter-spacing: 1px;
cursor: pointer;
color: #d80286;
animation: leave 0.4s forwards;
}
.slide_right:hover {
animation: hover 0.4s forwards;
}
@keyframes hover {
from {
box-shadow: inset 0 0 0 0.01px #d80286;
}
to {
box-shadow: inset 8.79928em 0 0 0.01px #d80286;
color: #fff;
}
}
@keyframes leave {
from {
box-shadow: inset -8.79928em 0 0 0.01px #d80286;
color: #fff;
}
to {
box-shadow: inset 0 0 0 0.01px #d80286;
}
}
<button class="button_slide slide_right">
Hello
</button>
推荐阅读
- r - 正则表达式“字符串中的每个字符都是数字吗?”
- python-3.x - Python:如何模拟多次调用的异步方法?
- python - Python @property 无法按我的意愿工作
- python - 如何避免多次尝试,除了块python
- html - 使用条件更改Angular中的元素文本
- javascript - 使用 Redux 进行“记忆”重要吗?
- arrays - perl6 在循环遍历数组时修改数组的一般方法
- c++ - 如何正确使用指针来计算平均值
- javascript - 应用 jquery 过滤器后,Google Maps API 3 平移到新的边界
- c - 根据面积对n个三角形的边进行排序