屏幕水平居中垂直居中

無論左邊的盒子放大仍是縮小,中間的盒子始終水平垂直居中css

html: html

  <div class="sidebar"><a href="###" class="click">點擊縮小左側導航寬度</a></div>
  <div class="notebar">
    <p>我始終在中間</p>
  </div>ide

css:htm

  .sidebar a {
    color:#fff;
  }
  .sidebar {
    background:blue;
    width:210px;
    height:100%;
    position:fixed;
    top:0;
    left:0;
  }
  .notebar {
    width:200px;
    height:190px;
    text-align:center;
    background:red;
  }
  .notebar p {
    margin:20px 0 0;
  }it

js:io

  $('.notebar').css('margin-left', ($(window).width() - $('.sidebar').width() - 200) / 2 + $('.sidebar').width());
  $('.notebar').css('margin-top', ($(window).height() - 190) / 2);function

  $('.click').on("click", function() {
    $('.sidebar').css('width', 50);
    $('.notebar').css('margin-left', ($(window).width() - $('.sidebar').width() - 200) / 2 + $('.sidebar').width());
    $('.notebar').css('margin-top', ($(window).height() - 190) / 2);
  })class