Search code examples
htmlcsspositionabsolute

How to customisly arrange Position absolute div tags


Here I'm Trying to display div#content_1 , div#content_2 and div#content_3 when clicking on corresponding item.I would like to show the last clicked item on top.Here if you click item3 once then you are not able to view item1 and item2 forever and if you click the item2 once you cannot see the item1 forever.please help me to fix this ,thanks in advance.(consider there we have large no of items )

function getElement(e) {
   var elem = document.getElementById(e);
   return elem;
}

getElement("item_1").addEventListener("click",function(){
    getElement("content_1").style.display = "block";});
    
getElement("item_2").addEventListener("click",function(){
    getElement("content_2").style.display = "block";});
    
getElement("item_3").addEventListener("click",function(){
    getElement("content_3").style.display = "block";});
ul,li{
  padding:0px;
  margin:0px;
  list-style:none;
 }
 div#menu{
  width:100%;
  height:50px;
 }
 div#menu>ul li{
  float:left;
  border:1px solid #000;
  padding: 2px;
  background-color:blue;
  color:#fff;
  margin:5px;
  cursor:pointer;
 }
  
 #content{
  width:100%;
  border:1px solid #000;
  height:200px;
  position :relative;
 }
 
 .abs{
  position:absolute;
  width:100px;
  height:100px;
  border:1px solid #000;
  display:none;
 }
 
 #content_1{
  background-color:red;
 }
 #content_2{
  background-color:green;
 }
 #content_3{
  background-color:yellow;
 }
<div id=menu>
  <ul>
    <li id="item_1">menu item1</li>
    <li id="item_2">menu item2</li>
    <li id="item_3">menu item3</li>
  </ul>
</div>
<div id="content">
  <div id="content_1" class="abs"></div>
  <div id="content_2" class="abs"></div>
  <div id="content_3" class="abs"></div>
</div>


Solution

  • Hope this will help you. if you don't want to hide div then just play with z-index.

    function getElement(e) {
       var elem = document.getElementById(e);
       return elem;
    }
    
    function zIndexZero() {
    document.querySelectorAll('.abs').forEach(function(item) {
      item.style.zIndex = 0;
    });
    }
    
    getElement("item_1").addEventListener("click",function(){
    
        zIndexZero();
        getElement("content_1").style.display = "block";
        getElement("content_1").style.zIndex = 99;
        });
        
    getElement("item_2").addEventListener("click",function(){
        zIndexZero();
        getElement("content_2").style.display = "block";
        getElement("content_2").style.zIndex = 99;
        
        });
        
    getElement("item_3").addEventListener("click",function(){
        zIndexZero();
        getElement("content_3").style.display = "block";
        getElement("content_3").style.zIndex = 99;
        });
    ul,li{
      padding:0px;
      margin:0px;
      list-style:none;
     }
     div#menu{
      width:100%;
      height:50px;
     }
     div#menu>ul li{
      float:left;
      border:1px solid #000;
      padding: 2px;
      background-color:blue;
      color:#fff;
      margin:5px;
      cursor:pointer;
     }
      
     #content{
      width:100%;
      border:1px solid #000;
      height:200px;
      position :relative;
     }
     
     .abs{
      position:absolute;
      width:100px;
      height:100px;
      border:1px solid #000;
      display:none;
     }
     
     #content_1{
      background-color:red;
     }
     #content_2{
      background-color:green;
     }
     #content_3{
      background-color:yellow;
     }
    <div id=menu>
      <ul>
        <li id="item_1">menu item1</li>
        <li id="item_2">menu item2</li>
        <li id="item_3">menu item3</li>
      </ul>
    </div>
    <div id="content">
      <div id="content_1" class="abs"></div>
      <div id="content_2" class="abs"></div>
      <div id="content_3" class="abs"></div>
    </div>