简体   繁体   English

滚动时将 div 粘在左侧

[英]Make a div stick to the left when scrolling

So I am trying to create a left-sided nav bar, but after scrolling I can't get it to correctly stay to the left.所以我试图创建一个左侧的导航栏,但滚动后我无法让它正确地留在左边。

I have tried using:我试过使用:

position: fixed;

and

position: absolute;

However it cuts the width of the DIV down completely.然而,它完全减少了 DIV 的宽度。

To get a view of what I'm working with go to: http://198.50.242.77/YouBB/要了解我正在使用的内容,请访问: http://198.50.242.77/YouBB/ : http://198.50.242.77/YouBB/

I'd prefer to use strictly CSS only but if I must use JS I'll use it.我更喜欢只使用严格的 CSS,但如果我必须使用 JS,我会使用它。

Thanks!谢谢!

position: fixed is what you want. position: fixed是你想要的。 This causes the element to be removed from the flow entirely and stay in the same position even after scrolling the page.这会导致元素从流中完全移除,并且即使在滚动页面后仍保持在同一位置。

position: absolute is similar, but it only removes an element from the flow. position: absolute类似,但它只从流中删除一个元素。 Scrolling a containing div (or in this case, the whole page) will still cause it to move.滚动一个包含的 div(或者在这种情况下,整个页面)仍然会导致它移动。

I opened up your web page in Chrome, and changed the styles for #navigation to:我在 Chrome 中打开了您的网页,并将#navigation的样式更改为:

background: white;
height: 100%;
text-align: center;
position: fixed;
width: 18.72%;

This does what you want.这做你想要的。 You will just need to position the rest of the content to the right.您只需要将其余内容放在右侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM