侧边栏将该幻灯片切换到右侧

时间:2015-08-17 07:32:58

标签: javascript jquery html css

下面是我的代码:

HTML

<div id="wrapper" style="background-color:red">
    <div id="sidebar-wrapper" style="background-color:yellow">sidebar
        <div id="result"></div>
    </div>
     <div id="header" class="container-fluid">
        <div class="navbar">
            <a href="#menu-toggle" id="menu-toggle" >Press</a>    
            <div>This is a serious health setback for me personally, but one of CN's core strengths is that we have a very experienced and tightly-knit senior  <span id="counterId"></span></div>
        </div>
    </div>
  </div>

CSS

#wrapper {
    padding-left: 0;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}
#wrapper.toggled {
    padding-left: 250px;
}
#sidebar-wrapper {
    z-index: 1000;
    position: fixed; 
    left: 250px;
    width: 0;
    height: 100%;
    margin-left: -250px;
    overflow-y:auto;
    background: #050545;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
    box-shadow: inset -10px 0px 10px -7px grey;
}
#wrapper.toggled #sidebar-wrapper {
    width: 250px;
}

JS

$("#menu-toggle").click(function(e) {
    e.preventDefault();
    $("#wrapper").toggleClass("toggled");
});

jsfiddle如下:

JSFIDDLE

正如大家们所看到的,当我按下链接时,它是右侧的侧边栏滑动。当我按下它时,它会挤压屏幕右侧的内容。

我想要的是内容的右侧向右滑动到屏幕后面,而不是将所有内容挤在一起。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

将固定width作为viewport#wrapper元素的宽度。

使用vw设置宽度。

  

视口宽度的1/100。

#wrapper {
  ...
  width: 100vw; // vw: Viewport width when page is loaded
}

More about vw

Demo

$("#menu-toggle").click(function(e) {
  e.preventDefault();
  $("#wrapper").toggleClass("toggled");
});
* {
  margin: 0;
  padding: 0;
}
#wrapper {
  width: 100vw;
  padding-left: 0;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}
#wrapper.toggled {
  padding-left: 250px;
}
#sidebar-wrapper {
  z-index: 1000;
  position: fixed;
  left: 250px;
  width: 0;
  height: 100%;
  margin-left: -250px;
  overflow-y: auto;
  background: #050545;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
  box-shadow: inset -10px 0px 10px -7px grey;
}
#wrapper.toggled #sidebar-wrapper {
  width: 250px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div id="wrapper" style="background-color:red">
  <div id="sidebar-wrapper" style="background-color:yellow">sidebar
    <div id="result"></div>
  </div>
  <div id="header" class="container-fluid">
    <div class="navbar"> <a href="#menu-toggle" id="menu-toggle">Press</a> 
      <div>This is a serious health setback for me personally, but one of CN's core strengths is that we have a very experienced and tightly-knit senior <span id="counterId"></span>

      </div>
    </div>
  </div>
</div>