JSFiddle - React, Tailwind, and code Playground
HTML
<div>
<div id="container" class="container">
<div id="box1" class="box box1">Div #1</div>
<div id="box2" class="box box2">Div #2</div>
<div id="box3" class="box box3">Div #3</div>
</div>
<div class="container xx">
<div id="box12" class="boxx box1">Div #12</div>
<div id="box22" class="boxx box2">Div #22</div>
<div id="box32" class="boxx box3">Div #32</div>
</div>
</div>
CSS
body {
padding: 0px;
}
.container {
position: absolute;
margin: 0px;
padding: 0px;
width: 100%;
/*height: 100%;*/
height: 300px;
overflow: hidden;
}
.container.xx {
top: 350px;
}
.box, .boxx {
position: absolute;
width: 50%;
height: 200px;
line-height: 200px;
font-size: 50px;
text-align: center;
border: 2px solid black;
left: 50%;
margin-left: -25%;
top: 50px;
}
.box1 {
background-color: green;
left: -150%;
}
.box2 {
background-color: yellow;
}
.box3 {
background-color: red;
left: 150%;
}
JavaScript
$('.box').click(function () {
$('.box').each(function () {
if ($(this).offset().left < 0) {
$(this).animate({
left: '50%',
}, 500);
} else if ($(this).offset().left > $('#container').width()) {
$(this).css("left", "-150%");
} else {
$(this).animate({
left: '150%',
}, 500);
}
});
});
$('.boxx').click(function() {
$('.boxx').each(function() {
if ($(this).offset().left < 0) {
$(this).css("left", "150%");
// } else if ($(this).offset().left > $('#container').width()) {
} else if ($(this).offset().left > $($(this).parent()).width()) {
$(this).animate({
left: '50%',
}, 500 );
} else {
$(this).animate({
left: '-150%',
}, 500 );
}
});
});