我做了一个按钮组,希望当用户选择上一个或下一个按钮的背景移动/滑动到选定的按钮时,我用纯css
做了这个效果,只是用jquery
来添加或删除active
类。现在的问题是,当你点击All
按钮,然后它工作正常,但如果你点击Used
幻灯片不工作在正确的位置。
为了达到这个效果,我使用了transition
和::before
,需要用纯css
或最少的jquery来解决这个问题,而不是用很多javascript
或jquery
代码。
移动此背景的逻辑是:
.RadioButton .btn:first-child::before {
right: 0;
transition: .3s all ease;
}
.RadioButton .btn:nth-child(2)::before {
transition: .3s all ease;
}
.RadioButton .btn:last-child::before {
left: 0;
transition: .3s all ease;
}
在.RadioButton .btn:nth-child(2)::before
上的问题我不能使用right:0
或left:0
,因为如果我使用每一个,滑动效果不能在正确的位置工作,有什么解决方案吗?
到目前为止,我尝试了以下内容:
$('.RadioButton').each(function(){
$(this).find('.btn').each(function(){
$(this).click(function(){
$(this).parent().find('.btn').removeClass('btn-active');
$(this).addClass('btn-active');
});
});
});
body {
direction: rtl;
}
.RadioButton .btn {
width: 33%;
float: left;
margin: 0;
box-sizing: border-box;
position: relative;
font-size: 11px;
min-height: 30px!important;
line-height: 30px;
border-radius: 5px;
overflow: hidden;
cursor: pointer;
text-align: center;
}
.btn-default {
border: 1px solid gray;
color: gray;
}
.btn-group>.btn:first-child {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.btn-group>.btn:nth-child(2) {
border-radius: 0;
}
.btn-group>.btn:last-child {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.RadioButton .btn::before {
content: "";
height: 100%;
width: 0%;
background: gray;
position: absolute;
top: 0;
transition: .3s all ease;
z-index: -1;
}
.btn-active::before {
width: 100%!important;
}
.RadioButton .btn:nth-child(2)::before {
right: 0;
transition: .3s all ease;
}
.RadioButton .btn:last-child::before {
left: 0;
transition: .3s all ease;
}
.btn-active:first-child::before {
left: 0;
transition: .3s all ease;
}
.btn-active:last-child::before {
left: 0;
transition: .3s all ease;
}
.btn.btn-default.btn-active {
color: white;
}
.btn-group {
clear: both;
margin-top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="RadioButton btn-group" id="searchType">
<a class="btn btn-default" data-val="0">Used</a>
<a class="btn btn-default" data-val="1">New</a>
<a class="btn btn-default btn-active" data-val="2">All</a>
</div>
https://stackoverflow.com/questions/47675041
复制相似问题