:之前和:伪元素以接收过渡效果



我正在尝试构建仅在菜单项中徘徊在悬停在盘旋上的平行四边形背景。对于形状,我正在使用:伪元素之前和:在伪元素之后,但是我不能对它们应用相同的过渡效果。有人知道我该怎么办来解决这个问题?

这是当前的代码:

div {
    float:left;   
    background-color:#fff; 
    margin: 20px;
    transition:.5s;
  
}
.testClass {
    margin-top: 0px;
    margin-left: 0px;
    padding: 5px 10px;
    display: block;
    background-repeat: no-repeat;
    background: #fff;
    position: relative;
    transition:.5s;
}
.testClass:hover {
    background: gold;
    transition:.5s;
}
.testClass:hover:before {
    content: '';
    position: absolute;
    top:0;
    left:-15px;
    width: 0px;
    height: 0px;
    border-style: solid;
    border-width: 0 0 29px 15px;
    border-color: transparent transparent gold transparent;
}
.testClass:hover:after {
    content: '';
    position: absolute;
    top:0;
    right:-15px;
    width: 0px;
    height: 0px;
    border-style: solid;
    border-width: 30px 15px 0 0;
    border-color: gold transparent transparent transparent;
}
<div >
    <div class="testClass">HOME</div>
    <div class="testClass">ABOUT US</div>
    <div class="testClass">CONTACT</div>
    <div class="testClass">LOGIN</div>
    <div class="testClass">SERVICES</div>
</div>

只使用一个元素创建形状的一种更简单的方法:

div {
  float: left;
  margin: 20px;
  transition: .5s;
}
.testClass {
  margin-top: 0px;
  margin-left: 0px;
  padding: 5px 10px;
  display: block;
  background: #fff;
  position: relative;
  transition: .5s;
  z-index: 0;
}
.testClass:before {
  content: '';
  position: absolute;
  z-index: -1;
  top: 0;
  left: -10px;
  right: -10px;
  bottom: 0;
  opacity: 0;
  background: gold;
  transform: skew(-20deg);
  transition: .5s;
}
.testClass:hover::before {
  opacity: 1;
}
<div>
  <div class="testClass">HOME</div>
  <div class="testClass">ABOUT US</div>
  <div class="testClass">CONTACT</div>
  <div class="testClass">LOGIN</div>
  <div class="testClass">SERVICES</div>
</div>

相关内容

  • 没有找到相关文章

最新更新