Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div id="wrapper">
    <div id="sidebar"></div>
    <div id="main"></div>
  </div>
  <button id="show">Show sidebar</button>
  <button id="show2">Show sidebar (with timeout)</button><br/>
  <button id="hide">Hide sidebar</button>
</body>
</html>
 
#wrapper {
  width: 400px;
  height: 100px;
  overflow: hidden;
  position: relative;
  
  &.show-sidebar {
    #sidebar {
      margin-left: 0;
    }
  }
}
#sidebar {
  width: 200px;
  height: 100%;
  margin-left: -200px;
  background-color: red;
  position: absolute;
  transition: margin 0.3s ease-out;
}
#main {
  width: 100%;
  height: 100%;
  background-color: blue;
}
 
$('#show').click(function() {
  // Isn't removeAttr synchronous?
  $('#sidebar').removeAttr('hidden');
  $('#wrapper').addClass('show-sidebar');
});
$('#show2').click(function() {
  $('#sidebar').removeAttr('hidden');
  
  // Why does this work then?
  setTimeout(function() {
    $('#wrapper').addClass('show-sidebar');
  }, 100); // Some arbitrary timeout
});
$('#hide').click(function() {
  $('#wrapper').removeClass('show-sidebar');
  
  setTimeout(function() {
    $('#sidebar').attr('hidden', 'hidden');
  }, 300); // Wait for CSS transition to finish
});
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers