d4n*_*est 5 css hover css3 css-transitions css-shapes
我正在尝试创建一个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;
}Run Code Online (Sandbox Code Playgroud)
<button class="button_sliding_bg">Buttontext</button>Run Code Online (Sandbox Code Playgroud)
您可以使用此答案中描述的技术:在悬停时从中心填充元素并倾斜伪元素,使其以倾斜方式填充按钮:
div {
position: relative;
display: inline-block;
padding: 15px 70px;
border: 5px solid #B17461;
color: #B17461;
font-size: 30px;
font-family: arial;
transition: color .5s;
overflow:hidden;
}
div:before {
content: '';
position: absolute;
top: 0; left: 0;
width: 130%; height: 100%;
background: #B17461;
z-index: -1;
transform-origin:0 0 ;
transform:translateX(-100%) skewX(-45deg);
transition: transform .5s;
}
div:hover {
color: #fff;
}
div:hover:before {
transform: translateX(0) skewX(-45deg);
}Run Code Online (Sandbox Code Playgroud)
<div>BUTTON</div>Run Code Online (Sandbox Code Playgroud)
不要忘记为浏览器支持添加供应商前缀(有关详细信息,请参阅canIuse).
我相信你实际上是在寻找最终状态,用背景颜色填充整个元素,而不是留下空隙.你也可以用它做linear-gradient背景图片和过渡的background-size,并background-position喜欢在下面的代码片段.
使用linear-gradientover伪元素或转换的一个缺点是浏览器支持较低但不需要额外的伪元素,因此可以留给其他用途.
.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;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<button class="button_sliding_bg">Buttontext</button>
<button class="button_sliding_bg">Button text lengthy</button>
<button class="button_sliding_bg">Button text <br> with line break</button>
<button class="button_sliding_bg">Button text <br> with <br> multiple <br> line <br>breaks</button>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2053 次 |
| 最近记录: |