TheGuy
TheGuy

Reputation: 477

creating toggle buttons to switch between div panels

My web application is made of various panels and a dashboard that has buttons to access each panel. So each button is a toggle between the purposed panel and the main panel.

For example, if we have 3 panels, panel1 is the default one and two buttons to toggle between other two panels the the default one:

button.panel2 should switch between panel1 and panel2

button.panel3 should switch between panel1 and panel3

I have almost achieved it but my code shows both panel2 and panel3 overlapping.

$(function () {
  $(".panel2").on("click", function() {
    var visibleObj = $('.mainSection div:visible');
    var inVisibleObj = $('.mainSection div:hidden');
    visibleObj.fadeOut(500, function() {
      inVisibleObj.fadeIn(500);
    });
  });
  $(".panel3").on("click", function() {
    var visibleObj = $('.mainSection div:visible');
    var inVisibleObj = $('.mainSection div:hidden');
    visibleObj.fadeOut(500, function() {
      inVisibleObj.fadeIn(500);
    });
  });
});
div.app {
  margin:50px auto;
  width: 400px;
  height: 400px;
  border-radius:10px;
  overflow: hidden;
  position: relative;
}
div.app > .blur {
  width: 100%;
  height: 100%;
  background: url(http://goo.gl/0VTd9W);
  -webkit-filter: blur(5px);
}
div.mainSection, div.dashboard{
  position: absolute;
  left: 0px;
  text-align:center;
  color:#fff;
  font-size:20px;
}
div.mainSection{
  width:100%;
  height:85%;
  background:rgba(0,0,0,0.5);
  top:0;
}
div.dashboard{
  width:100%;
  height:15%;
  background:rgba(255,0,0,0.5);
  bottom:0;
}
div.mainSection > .panel1,
div.mainSection > .panel2,
div.mainSection > .panel3 {
  width: 100%;
  Height: 100%;
  Background: rgba(0, 0, 0, 0.5);
  position: absolute;
  left: 0px;
  top: 0px;
}
div.mainSection > .panel3 > p{
  margin-top:80px;
}

.grid-button {
  background: none;
  border: none;
  padding: 3px;
  width: 100%;
}

.grid {
  display: inline-block;
  height: 4px;
  position: relative;
  width: 32px;
  transition: all 0.3s ease-in-out;
}
.grid:after, .grid:before {
  content: '';
  position: absolute;
  background-color: #FFF;
  display: inline-block;
  height: 4px;
  left: 0;
  width: 32px;
  transition: all 0.3s ease-in-out;
}

.grid.open {
  background-color: #FFF;
}
.grid.open:after {
  top: 10px;
}
.grid.open:before {
  top: -10px;
}

.grid.close {
  background-color: transparent;
  transform: scale(0.9);
}
.grid.close:after, .grid.close:before {
  top: 0;
  transform-origin: 50% 50%;
}
.grid.close:before {
  transform: rotate(135deg);
}
.grid.close:after {
  transform: rotate(45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="app">
    <div class="blur"></div>
    <div class="mainSection">
        <div class="panel1">Panel1</div>
        <div class="panel2" style="display: none;">Panel2</div>
        <div class="panel3" style="display: none;"><p>Panel3</p></div>
    </div>
    <div class="dashboard">
        <button class="panel2">button1</button>
        <button class="panel3">button2</button>
    </div>
</div>

So I want to make button1 responsible to switch between panel 1 and 2. and button2 to switch between panel 1 and 3.

Any idea how to do it?

Upvotes: 0

Views: 2353

Answers (2)

Enes Tufekci
Enes Tufekci

Reputation: 87

I think you need something like this. just these jquery code will be useful.

var currentPanel = 1;

$('.panelControlBtn').on("click", function() {
  var ID = $(this).attr('data-id');
  if (ID != currentPanel) {
    $(".panel").fadeOut('fast', function() {
      $("#panel" + ID).fadeIn('fast');
    });
    currentPanel = ID;
  }



});

Upvotes: 2

digglemister
digglemister

Reputation: 1487

How about this? https://jsfiddle.net/oez0488h/53/

$("button.panel2").on("click", function() {
    var visibleObj = $('.mainSection div:visible');
    if ($("div.panel2").css("display") == "none") {
        var inVisibleObj = $("div.panel2")
    }
    else {
        var inVisibleObj = $("div.panel1")
    }
    visibleObj.fadeOut(500, function() {
        inVisibleObj.fadeIn(500);
    })
});

$("button.panel3").on("click", function() {
    var visibleObj = $('.mainSection div:visible');
    if ($("div.panel3").css("display") == "none") {
        var inVisibleObj = $("div.panel3")
    }
    else {
        var inVisibleObj = $("div.panel1")
    }
    visibleObj.fadeOut(500, function() {
        inVisibleObj.fadeIn(500);
    })
});

Upvotes: 1

Related Questions