制作背景幻灯片动画

时间:2017-12-06 13:06:20

标签: javascript jquery html css css3

我创建了一个按钮组,并希望当用户选择上一个或下一个按钮的每个按钮背景移动/滑动到所选按钮时,我使用纯css创建此效果,并使用jquery添加或删除active课程。现在问题是当你点击All按钮,然后New它工作正常,但如果你点击Used幻灯片不能正常工作。

对于我使用transition::before的效果,需要使用纯css或最小jquery来解决此问题,而不是使用javascriptjquery码。

移动此背景的逻辑是:

.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:0left: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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

在最后一个按钮上只设置一个伪。 现在,使用sibbling选择器和活动类将其移动到左侧,作为适当的。 (参见CSS中的最后两个样式)

&#13;
&#13;
$('.RadioButton').each(function(){
	$(this).find('.btn').each(function(){
		$(this).click(function(){
			$(this).parent().find('.btn').removeClass('btn-active');
			$(this).addClass('btn-active');
		});
	});
});
&#13;
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;
    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;
}
.btn.btn-default.btn-active {
    color: white;
}

.btn-group {
    clear: both;
    margin-top: 10px;
}

.RadioButton .btn:nth-child(3)::before {
    content: "";
    height: 100%;
    width: 100%;
    background: gray;
    position: absolute;
    top: 0;
    transition: .3s all ease;
    z-index: -1;
    left: 0px;
}

.btn-active:first-child ~ .btn::before {
    transform: translateX(calc(-4px - 200%));
}
.btn-active:nth-child(2) ~ .btn::before {
    transform: translateX(calc(-2px - 100%));
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

更改位置中心

&#13;
&#13;
$('.RadioButton').each(function(){
	$(this).find('.btn').each(function(){
		$(this).click(function(){
			$(this).parent().find('.btn').removeClass('btn-active');
			$(this).addClass('btn-active');
		});
	});
});
&#13;
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;
    left: 50%;
    transform: translateX(-50%);
}

.btn-active::before {
    width: 100%!important;
}

.RadioButton .btn:nth-child(2)::before {
 
 transition: .3s all ease;
}

.RadioButton .btn:last-child::before {
  
  transition: .3s all ease;
}

.btn-active:first-child::before {
 
 transition: .3s all ease;
}

.btn-active:last-child::before {
  
  transition: .3s all ease;
}

.btn.btn-default.btn-active {
    color: white;
}

.btn-group {
    clear: both;
    margin-top: 10px;
}
&#13;
<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>
&#13;
&#13;
&#13;