在悬停时填充元素与倾斜的背景 [英] Fill element with slanted background on hover

查看:84
本文介绍了在悬停时填充元素与倾斜的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建 CSS按钮悬停效果



如何计算悬停效果:





截图1:实际效果如何。



截图2:我想让悬停效果看起来像斜边。



  .button_sliding_bg {color:#31302B;背景:#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 class = button_sliding_bg> Buttontext< / button>  

解决方案

我相信你实际上是在寻找结束状态,用背景颜色填充整个元素,而不是留下空隙。您也可以使用 linear-gradient 背景图片,并转换其 background-size



使用 linear-gradient的一个缺点 over伪元素或变换是,浏览器支持较低,但它不需要额外的伪元素,因此可以留给其他用途以供其他用途。



div class =snippetdata-lang =jsdata-hide =false>

  .button_sliding_bg {color:#31302B;背景:#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;背景图像:线性梯度(135deg,#31302B 50%,透明度51%); background-size:100px 100px; / *一些初始大小得到倾斜的外观* / background-position:-50px -50px; / *负定位隐藏它最初* / background-repeat:no-repeat; transition:all ease 0.8s;}。button_sliding_bg:hover {background-size:200%200%; / * 200%因为渐变颜色只有50%* / background-position:0px 0px; / *让它完全进入视图* / color:#FFF;}  

 < 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>< button class =button_sliding_bg>按钮文字< br& with line break< / button>< button class =button_sliding_bg>按钮文字< br>与< br>多个< br>线< br>休息符< / button>  


I'm trying to create an CSS button hover effect. But I didn't manage to fill the element with a slanted shape.

How the hover effect was planned:

Screenshot 1: How it looks actually.

Screenshot 2: How I want the hover effect to look like with slanted side.

.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 class="button_sliding_bg">Buttontext</button>

解决方案

I believe that you are actually looking for the end state to fill the entire element with background color and not leave the gap. You could also do it with linear-gradient background images and transition their background-size and background-position like in the below snippet.

One disadvantage of using linear-gradient over pseudo-elements or transforms is that the browser support is lower but it doesn't need extra pseudo-elements and so can leave them spare for other use.

.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;
}

<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>

这篇关于在悬停时填充元素与倾斜的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆