[英]Button underline animation with css and html
I have two buttons that have underline animation when I click them.我有两个按钮,当我点击它们时有下划线动画。 What I want to achieve is that one button, after being clicked, stays underlined, and, if I click the other one, the first removes the underlining and the other becomes underlined.我想要实现的是,一个按钮在被点击后保持带下划线,如果我点击另一个,第一个删除下划线,另一个变成下划线。
A problem is also that when I release the mouse, the underlining fades out.还有一个问题是,当我松开鼠标时,下划线淡出。 I don't know how to do it.我不知道该怎么做。 Here is the code I have for now:这是我现在的代码:
HTML: HTML:
<div id = 'hey'>
<div class = 'b' id = 'b1' href="#">Button one</div>
<div class = 'b' id = 'b2' href="#">Button Two</div>
</div>
CSS: 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;
}
Best is to use jQuery:最好是使用jQuery:
$('#hey').find('.b').each(function() {
$(this).click(function() {
if(!$(this).hasClass('active')) {
$('#hey').find('.b').each(function() {$(this).removeClass('active');});
$(this).addClass('active');
}
else {
$(this).removeClass('active');
}
});
});
Then just use the class active
for the underlining.然后只需使用active
类作为下划线。
Please include jquery cdn link then请包括jquery cdn链接然后
Try this script试试这个脚本
$(".b").click(function() {
if(!$(this).hasClass('active')) {
$('#hey').find('.b').each(function() {$(this).removeClass('active');});
$(this).addClass('active');
}
else {
$(this).removeClass('active');
}
});
Aldo add this css奥尔多添加这个css
.b.active:after {
width: 100%;
left: 0;
}
Try this using jquery:使用 jquery 试试这个:
$(document).ready(function(){ $('#hey .b').click(function(){ $('#hey .b').removeClass('active'); $(this).addClass('active'); }); });
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; } #hey .b { color: #fff; text-transform: uppercase; text-decoration: none; letter-spacing: 0.15em; cursor: pointer; display: inline-block; padding: 15px 20px; position: relative; } #hey .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; } #hey .b.active:after { width: 100%; left: 0; }
<div id = 'hey'> <div class="active b" id = 'b1' href="#">Button one</div> <div class="b" id = 'b2' href="#">Button Two</div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.