简体   繁体   中英

Sliding a div without moving neighbouring content

I'm trying to create a side bar which will slide in and show the content when a user clicks a button.

the problem that I'm having is when its clicked all the content on the header, body and footer are being moved.

I want the sliding div to slide on top of menu and content without moving it, creating an overlay when clicked

Check the fiddle http://jsfiddle.net/livewirerules/tsmpraym/9/

Below is what i have tried so far

 jQuery(document).ready(function($) { $("#side").click(function() { $('#slidable').animate({ width: 'toggle' }); }); }) 
 #menu { background-color: green; height: 100px; } #footer { background-color: blue; height: 100px; } #content { background-color: red; height: 400px; } #side { float: left; width: 50px; height: 50px; background: #BBB; } .hide { display: none; } #slidable { float: left; height: 200px; background: #888; width: 200px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> <div id="slidable" class="hide">Foobar</div> <div id="side">+</div> <div id="menu"> the menu </div> <div id="content"> content </div> <div id="footer"> the footer </div> 

 jQuery(function($) { // yep. DOM ready $("#side").click(function() { $('#slidable').toggleClass("open"); }); }) 
 body{margin:0;} /* yey? */ #menu { background-color: green; height: 100px; } #footer { background-color: blue; height: 100px; } #content { background-color: red; height: 400px; } #side { /*float: left; huh? */ position:absolute; /* add this */ left:100%; /* add this */ width: 50px; height: 50px; background: #BBB; } /*.hide { display: none;} */ #slidable { /*float: left; why but why */ position: fixed; /* add this */ top:0; height: 100vh; background: #888; width: 200px; right: 100%; /* add this */ transition: 0.4s; } #slidable.open{ right: calc(100% - 200px); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> <div id="slidable"> Foobar <div id="side">+</div> </div> <div id="menu"> the menu </div> <div id="content"> content </div> <div id="footer"> the footer </div> 

You should put the position absolute and keep available the toggle button too.

  jQuery(document).ready(function($) { $( "#side" ).click(function() { $('#slidable').animate({width: 'toggle'}); $('#side').toggleClass('keepRight'); }); }) 
 #menu { background-color:green; height:100px; } #footer { background-color:blue; height:100px; } #content { background-color:red; height:400px; } #side{ float:left; width:50px; height:50px; background:#BBB; transition: all .5s; transition-timing-function: swing; } .keepRight{ margin-left: 200px; transition: all .5s; transition-timing-function: swing; } .hide{ display:none; } #slidable{ float:left; height:200px; background:#888; width:200px; position: absolute; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <div id="slidable"class="hide">Foobar</div> <div id="side">+</div> <div id="menu"> the menu </div> <div id="content"> content </div> <div id="footer"> the footer </div> 

The CSS property position:absolute; will have the menu position absolutely on top of other elements. This way it will not re-flow the content of elements beneath it.

http://www.w3schools.com/css/css_positioning.asp

However, in your example, this will cover the button used to open it. You may want to add an interior button to close it.

I've updated your jsfiddle: http://jsfiddle.net/tsmpraym/27/

Inside the menu div you can add another close button:

<div id="slidable"class="hide">Foobar<div id="close">+</div></div>

You can add the same properties to it by appending its ID (or refactor into a common class, rather than by ID):

#side, #close{
  float:left;
  width:50px;
  height:50px;
  background:#BBB;
}

To make it hang off the side, you can make it positioned absolutely with a negative right value:

#close {
  position: absolute;
  top:0px;
  right:-50px;
}

The close button also needs to trigger a toggle:

jQuery(document).ready(function($) {
  $('#side').click(function() {
   $('#slidable').animate({width: 'toggle'});
  });
  $('#close').click(function() {
   $('#slidable').animate({width: 'toggle'}); 
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM