我正在尝试创建一个CSS按钮悬停效果.但我没有设法用倾斜的形状填充元素.
如何计划悬停效果:
屏幕截图1:它实际上看起来如何.
屏幕截图2:我希望悬停效果看起来像倾斜的一面.
.button_sliding_bg {
color: #31302B;
background: #FFF;
padding: 12px 17px;
margin: 25px;
font-family: 'OpenSansBold',sans-serif;
border: 3px solid #31302B;
font-size: 14px;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
border-radius: 2px;
display: inline-block;
text-align: center;
cursor: pointer;
Box-shadow: inset 0 0 0 0 #31302B;
-webkit-transition: all ease 0.8s;
-moz-transition: all ease 0.8s;
transition: all ease 0.8s;
}
.button_sliding_bg:hover {
Box-shadow: inset 200px 0 0 0 #31302B;
color: #FFF;
}
最佳答案
我相信你实际上是在寻找最终状态,用背景颜色填充整个元素,而不是留下空隙.您也可以使用线性渐变背景图像并转换背景大小和背景位置,如下面的代码段所示.
使用线性渐变而不是伪元素或变换的一个缺点是浏览器支持较低但不需要额外的伪元素,因此可以将它们留作其他用途.
.button_sliding_bg {
color: #31302B;
background: #FFF;
padding: 12px 17px;
margin: 25px;
font-family: 'OpenSansBold',sans-serif;
border: 3px solid #31302B;
font-size: 14px;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
border-radius: 2px;
display: inline-block;
text-align: center;
cursor: pointer;
background-image: linear-gradient(135deg,#31302B 50%,transparent 51%);
background-size: 100px 100px; /* some initial size to get the slanted appearance */
background-position: -50px -50px; /* negative positioning to hide it initially */
background-repeat: no-repeat;
transition: all ease 0.8s;
}
.button_sliding_bg:hover {
background-size: 200% 200%; /* 200% because gradient is colored only for 50% */
background-position: 0px 0px; /* bring it fully into view */
color: #FFF;
}