【发布时间】:2020-03-29 00:18:13
【问题描述】:
当我单击它们时,我有两个带有下划线动画的按钮。我想要实现的是一个按钮,在被点击后,保持下划线,如果我点击另一个,第一个删除下划线,另一个变成下划线。
还有一个问题是,当我松开鼠标时,下划线会淡出。我不知道该怎么做。这是我现在拥有的代码:
HTML:
<div id = 'hey'>
<div class = 'b' id = 'b1' href="#">Button one</div>
<div class = 'b' id = 'b2' href="#">Button Two</div>
</div>
CSS:
body,html {
font: bold 14px/1.4 'Open Sans', arial, sans-serif;
background: #000;
}
#hey {
margin: 150px auto 0;
padding: 0;
list-style: none;
display: table;
width: 600px;
text-align: center;
}
.b {
color: #fff;
text-transform: uppercase;
text-decoration: none;
letter-spacing: 0.15em;
display: inline-block;
padding: 15px 20px;
position: relative;
}
.b:after {
background: none repeat scroll 0 0 transparent;
bottom: 0;
content: "";
display: block;
height: 2px;
left: 50%;
position: absolute;
background: #fff;
transition: width 0.3s ease 0s, left 0.3s ease 0s;
width: 0;
}
.b:active:after {
width: 100%;
left: 0;
}
【问题讨论】:
-
如果 jQuery 有用,我会很乐意使用它
-
您实际上可以通过使用复选框和 CSS 兄弟选择器使用纯 CSS 来解决这个问题。 stackoverflow.com/a/59001709/5526624
-
另外,这个东西不需要 JQuery。使用 vanilla JS 很简单,但现在您已经选择了正确的答案。