简体   繁体   中英

Problems with layout css and javascript

I have the following layout: 布局

On the left side I have a menu and big gray part on the right side is the body content. The problem is on the left menu I have a bunch of buttons. I want this menu to be fixed position and body scrollable. I Have the following css:

#menu {
position: fixed;
}
#content {
position: inherit;
margin-left:300px;
}

The problem is that on the red part of my menu all button unavailable, I can't click on it. looks like body overrides the menu.

Any ideas what the problem might be?

Thanks

In order to fix it to the top and not scroll, you don't use position: fixed; . You need to use position: absolute; . If you don't want it at the very top, then you use position: relative; and place it inside an element.

Then, in order to scroll, you use position: fixed; .

When you use position: fixed , it places the element fixed within the visible page .

However, when you use position: absolute , what this does is put it on an absolute position on the page regardless of scroll . For example, if you added the css top:0; then it would be 0 pixes from the absolute top of page, and if you scroll down it will disappear from view because it is all the way at the top of the actual page, not just the top of the visible page.

I understand it seems a bit counter-intuitive to you. However, you can see it working in the jsbin below.

Working jsbin:
http://jsbin.com/Uwuyuha/1

page.html

<body>
  <div id="container">
    <div id="menu">
            1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>
    </div>
    <div id="content">
    </div>
  </div>
</body>

style.css

body {
  width: 100%;
  height: 100%;
}
#container {
  width: 1000px;
  height: 1000px;
}
#menu {
  width: 250px;
  height: 2000px;
  position: fixed;
  background: #999;
}
#content {
  width: 650px;
  height: 300px;
  position: absolute;
  margin-left: 251px;
  background: #444;
}

Including the html would give a better sense of the stacking order and likely yield a better answer. Given what you've provided, this should fix:

#menu {
  position: fixed;
  z-index: 1;
}

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