문제

I'm trying to make a menu bar float constantly at the top of the browser, so when they scroll down the page the menu bar remains at the top.

How could I go about doing this?

Regards,

도움이 되었습니까?

해결책

You can do this using the CSS position property. For example:

#menu {
  height: 50px;
  left: 0;
  position:fixed;
  top: 0;
}

References:

The first place I noticed this used effectively is on the Perldoc site. If you have to scroll, the #content_header element uses a combination of CSS and JS to keep the element visible on the page.

다른 팁

The CSS tag position: fixed; will make it stay in the same position on the screen, even if scrolling. Use that and then position it with top/right/bottom/left as shown below. z-index will affect how high it is 'stacked'. That is, an element with a z-index of 1 will be beneath an element of a z-index of 100.

div.float {
    position: fixed;
    top: 10px;
    left: 25px;
    z-index: 9001;
}

jsfiddle.net was down earlier, but it's back up. Here's an example of a floating menu that is static until you scroll to a certain point: http://jsfiddle.net/2rhrc/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top