gpt4 book ai didi

css - :before and :after pseudo elements to receive transition effect

转载 作者:太空宇宙 更新时间:2023-11-03 22:29:00 24 4
gpt4 key购买 nike

我正在尝试构建一个仅在悬停在菜单项中时出现的平行四边形背景。对于形状,我使用了 :before 和 :after 伪元素,但是我不能对它们应用相同的过渡效果。有谁知道我可以做些什么来解决这个问题?

这是目前为止的代码:

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>

关于css - :before and :after pseudo elements to receive transition effect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49544102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com