简体   繁体   English

#sidebar位置:固定于某些滚动

[英]#sidebar position:fixed at certain scrolling

Please look at this: http://i.stack.imgur.com/gDFpq.png 请查看以下内容: http : //i.stack.imgur.com/gDFpq.png

My aim is to have the sidebar as it is in the diagram but once it reaches the top of the window (when the user scrolls down), it changes to position:fixed and changes margin-top:0 . 我的目标是使边栏保持在图中的状态,但是一旦到达窗口顶部(当用户向下滚动时),它就会更改为position:fixed并更改margin-top:0 I've looked at a tutorial online to this and learned that position:absolute may be useful but I have honestly no clue how to do this so any help would be useful. 我在网上看了一个教程,并了解了这一position:absolute可能有用,但老实说我不知道​​如何做到这一点,因此任何帮助都是有用的。 If you need a bit more detail on what I mean because you don't understand that please do ask :) 如果您因为我不了解我的意思而需要更多细节,请询问:)

Any help would be appreciated. 任何帮助,将不胜感激。

To detect when it reaches the top of the window, you can use the window.scroll event. 要检测何时到达窗口顶部,可以使用window.scroll事件。

Here's a nice tutorial that teaches you exactly what you want to do: http://css-tricks.com/scrollfollow-sidebar/ 这是一个很好的教程,可以教您确切要做什么: http : //css-tricks.com/scrollfollow-sidebar/

Your sidebar will have a variable height until the logo is scrolled out of sight. 您的侧边栏将具有可变的高度,直到徽标滚动到视线之外。 Instead of using position:absolute , keep it at position:fixed , check the remaining height of the logo and extract this from the sidebar 's height. 代替使用position:absolute ,将其保持在position:fixed ,检查徽标的剩余高度并将其从侧边栏的高度中提取出来。

Here's a fiddle: http://jsfiddle.net/2afv13z3/ 这是一个小提琴: http : //jsfiddle.net/2afv13z3/

When keeping the sidebar at bottom:0; 侧栏保持在bottom:0; and top property based on the window scroll position, you'll always have it at 100% under your logo . 以及基于窗口滚动位置的top属性,您将始终在徽标下将其设置为100%。

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

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