jquery - 响应式侧边栏也是固定的

标签 jquery html css responsive-design css-float

我有一个响应式设计,其中我的边栏占 25%,我的内容占 75%。我的问题是,当我滚动页面时,侧边栏不固定并且不随页面向下滚动。

我尝试将其设置为 position:fixed 但随后我失去了对内容的整体响应百分比。

我怎样才能同时实现这两个目标?

fiddle : http://codepen.io/anon/pen/oXMmOz

enter image description here

enter image description here

enter image description here

enter image description here

HTML

  <div class="container max-width">
    <div class="sidebar"></div>
    <div class="content"></div>
  </div>

CSS

.max-width {
  max-width: 1300px;
  margin: 0 auto;
  position: absolute;
  top: 0px;
  bottom: 0px;
  left: 0px;
  right: 0px;
  margin-top: 54px;
}

.sidebar {
  background-color: red;
  width: 25%;
  padding-top: 0;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;  
}

.content {
  background-color: black;
  width: 75%;
  margin-left: 25%;
  padding-top: 15px;
}

@media(max-width: 768px) {
  .sidebar {
    left: -200px;
    position: fixed;
  }
  .content {
    width: 100%;
    margin-left: auto;
  }
}

最佳答案

您将侧边栏定位为绝对的。这将忽略父元素并使用主体作为引用。你应该使用 position: relative。

你还应该将内容向右浮动,而不是使用 margin left 25%..

我会为你制作 fiddle 。我以前从未使用过 codepen。

好的,看看这个。这接近您要找的东西吗?

http://codepen.io/anon/pen/pJZYvB

body, html {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}
.max-width {
  max-width: 1300px;
  width: 100%;
  height: 100%;
  margin: 0;
  position: absolute;
  top: 0;

  left: 0px;




}

.sidebar {
  background-color: red;
  width: 25%;
  padding-top: 0;

  position: fixed;
  left: 0;
  top: 0;
  bottom: 0; 
  height: 100%;

}

.content {
  background-color: black;
  width: 75%;
  display: inline-block;
  float: right;
  padding-top: 15px;
}

@media(max-width: 768px) {
  .sidebar {
    left: -200px;
    position: fixed;
  }
  .content {
    width: 100%;
    margin-left: auto;
  }
}

关于jquery - 响应式侧边栏也是固定的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31507842/

相关文章:

javascript - 将按钮添加到新元素

css - 有没有办法只使用 CSS 让背景图像在固定元素内滚动?

c# - 无法通过查询字符串传递 # 字符

javascript - jQuery .css() 不适用于 IE 6、7、8 和 Firefox,但适用于 Chrome、Safari、Opera

javascript - 如何点击div下的元素?

javascript - 在 html5 Canvas 中为自定义形状设置动画

javascript - 关于从 Sprite 添加 100+ 图像然后将它们垃圾处理的问题(长)

android - 为什么某些 css 代码在 android 4 或更低版本上不起作用?

javascript - 动态输入文本框 ID 与正则表达式格式不匹配。显示未定义

php - 在 php 中动态更改 css?