JSFiddle - React, Tailwind, and code Playground

by Mi_Creativity

HTML

<ul>
  <li id="li-A">item 1</li>
  <li id="li-B">item 2</li>
  <li id="li-C">item 3</li>
  <li id="li-D">item 4</li>
</ul>
<div class="sliding-divs" id="div-A">
  DIV A
</div>
<div class="sliding-divs" id="div-B">
  DIV B
</div>
<div class="sliding-divs" id="div-C">
  DIV C
</div>
<div class="sliding-divs" id="div-D">
  DIV D
</div>

CSS

body {
  overflow-x: hidden;
}

ul {
  list-style: none;
  padding: 0;
}

li {
  width: 100px;
  height: 25px;
  margin: 2px 0;
  color: white;
  padding: 3px;
  text-align: center;
  background-color: green;
  cursor:pointer;
}

.sliding-divs {
  position: absolute;
  width: 500px;
  line-height: 250px;
  background-color: orange;
  font-size: 30px;
  border: 2px gold solid;
  text-align: center;
  display: inline-block;
  top: 150px;
  left: -510px;
}

JavaScript

// initializing
var prevID = '',
	divs = $('.sliding-divs');

$("li").on("click", function() {
  var theID, theDiv, theDivW, theCenter;
  
  // get the id letter from the li, then pick the corresponding sliding
  // div depending on its value.
  theID = $(this).attr('id');
  theID = theID.replace('li-', '');
  theDiv = $('#div-' + theID);
  
  // get the divs width to slide it into the center of the view
  theDivW = theDiv.width();
  theCenter = $(window).width()/2 - theDivW/2;
  
  // if the user didn't click the link which its slide already 
  // in the view, this to avoid sliding out and in same div.
  if(theID != prevID){
  	if (prevID == '') {
    
    	// if we don't have a previously slided in div, we just slide 
      // the just click link's div into the view
      theDiv.animate({'left': theCenter}, 1000);
    } else {
    	
      // animated the previous div to the right out of the view, then
      // move all divs to their original position out from the left
      // this is because if we don't do this, an already slided div 
      // will later be slided in from right instead in from left
      // because we have already changed its position.
      // slide the just clicked link's div into the view from left
      $('#div-' + prevID).animate({'left': '110%'}, 800);
      divs.css({'left':-(theDivW + 100)});
      theDiv.animate({'left': theCenter}, 1000);
    }
  }
  prevID = theID;
});