Padideh
Padideh

Reputation: 149

Making background slide animation

I made a button group, and want when user select each button background of previous or next button move/slide to selected one, i made this effect with pure css and just used jquery to add or remove active class. now the problem is when you click on All button, then New it works fine, but if you click on Used the slide not work in right position.

For this effect i used transition and ::before, need to solve this with pure css or minimum jquery, not a lot of javascript or jquery codes.

The logic to move this background is:

.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;
}

The problem on .RadioButton .btn:nth-child(2)::before i can't use right:0 or left:0 because if i use each one, the slide effect not work in right position, any solution?

What i tried so far:

$('.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>

Upvotes: 5

Views: 1536

Answers (2)

vals
vals

Reputation: 64164

Set only one after pseudo, on the last button. Now, use sibbling selectors and the active class to move it to the left, as appropiate. (see the last 2 styles in the CSS)

$('.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;
    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%));
}
<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>

Upvotes: 5

Lalji Tadhani
Lalji Tadhani

Reputation: 14149

change position center

$('.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;
    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;
}
<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>

Upvotes: -1

Related Questions