javascript - Onclick changing content on <div> - Stack Overflow

Is there a way I can make the on the right appear one at a time? And show by sliding?Here's the co

Is there a way I can make the on the right appear one at a time? And show by sliding?

Here's the code I'm using to call the

<button onClick="div1()">Try it</button>
<button onClick="div2()">Try it</button>
<button onClick="div3()">Try it</button>

Here is the script I am using

<script>
function div1() {
    var x = document.getElementById('myDIV');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}

function div2() {
    var x = document.getElementById('myDIV2');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}

function div3() {
    var x = document.getElementById('myDIV3');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}
</script>

I want to make my site look like this

Is there a way I can make the on the right appear one at a time? And show by sliding?

Here's the code I'm using to call the

<button onClick="div1()">Try it</button>
<button onClick="div2()">Try it</button>
<button onClick="div3()">Try it</button>

Here is the script I am using

<script>
function div1() {
    var x = document.getElementById('myDIV');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}

function div2() {
    var x = document.getElementById('myDIV2');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}

function div3() {
    var x = document.getElementById('myDIV3');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}
</script>

I want to make my site look like this

Share Improve this question edited Apr 17, 2017 at 9:03 MLavoie 9,89641 gold badges42 silver badges59 bronze badges asked Apr 17, 2017 at 6:59 Keith MercadoKeith Mercado 751 silver badge6 bronze badges 3
  • 1 Wele to SO.I think you need to be more descriptive to get an answer here. Read stackoverflow./help/how-to-ask – Santhosh Commented Apr 17, 2017 at 7:04
  • .slideToggle() could be used to show by sliding – Mark Commented Apr 17, 2017 at 7:07
  • where do i put the .slideToggle()? – Keith Mercado Commented Apr 17, 2017 at 7:15
Add a ment  | 

2 Answers 2

Reset to default 3

By creating a function where you send the div number ( 1 , 2 , 3 ...) you can slide the concerned div and hide all the others .

Try the bellow snippet : ( using jquery ) ,

function toggleDiv(divNum) {
    
    $("#close").hide();
    $(".slide").animate({right:'-200'},350);
    if($("#div"+divNum)) {
        
        $("#div"+divNum).animate({right:'0'},350,function(){$("#close").show();});
    }
}

$(document).ready(function(){
   $("#close").on("click",function(e){
      $(".slide").animate({right:'-200'},350);
      $(this).hide()
   })

})
.slide {
  width:200px;
  height:100%;
  position:absolute;
  right:-200px;
  top:0;
  background:#d2d2d2;
}

#close {
  position:absolute;
  right:10px;
  top:10px;
  z-index:10;
  display:none;
}

#right-content {
  position:absolute;
  right:0;
  top:0;
  overflow:hidden;
  width:200px;
  height:100%;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onClick="toggleDiv(1)">Try it 1</button>
<button onClick="toggleDiv(2)">Try it 2</button>
<button onClick="toggleDiv(3)">Try it 3</button>

<div id="right-content">
  <div id="close">X</div>
  <div class="slide" id="div1">content 1</div>
  <div class="slide" id="div2">hey I'm content 2</div>
  <div class="slide" id="div3">Now it's content 3</div>
<div>

Do like this:

<div id="rightSideDiv" ></div>
<input type="hidden" id="myHiddenField1" value="<b> Info of div 1</b>" />
<input type="hidden" id="myHiddenField2" value="<b> Info of div 2</b>" />
<input type="hidden" id="myHiddenField3" value="<b> Info of div 3</b>" />

<button onClick="doDisplay('myHiddenField1')">Try it</button>
<button onClick="doDisplay('myHiddenField2')">Try it</button>
<button onClick="doDisplay('myHiddenField3')">Try it</button>

<script>
function doDisplay(hiddenFileldID) {
    var x = document.getElementById(hiddenFileldID).value;
    document.getElementById("rightSideDiv").innerHTML = x;
}
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744790005a4593859.html

相关推荐

  • javascript - Onclick changing content on &lt;div&gt; - Stack Overflow

    Is there a way I can make the on the right appear one at a time? And show by sliding?Here's the co

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信