jQuery - DIV to move with scrolling motion and sti

2019-01-23 07:25发布

问题:

There may be already a jQuery plugin which can achieve this, but I can't find one to do exactly what I'm after. If there is, just point me to the tutorial, thanks.

My problem I have is that I have very long page content, and my sidebar is not visible when you are scrolled near the bottom of the page.

So I would like to make my #sidebar div to stick to the top and bottom of my browser window as you scroll up and down the page.

My sidebar height is longer than your typical screen resolution, so I need the bottom of sidebar to sticky to the bottom of the browser window as well as the top of the browser.

So as you begin to scroll down, the side bar will scroll like normal, but when you reach the end the sidebar, it sticks and will not scroll, and as you begin to scroll up, the sidebar will follow until the top of sidebar reaches the browser, then it sticks. Vice Versa.

Is this possible?

I have created a jsfiddle of simple design layout which is central. I have added a dotted border to the sidebar so you now where the sidebar should stick.

http://jsfiddle.net/motocomdigital/7ey9g/5/

Any advice, or you know a online tutorial or demo, would most awesome!


UPDATE

Please see this attempt by @Darek Rossman

http://jsfiddle.net/dKDJz/4/

He's got the basic idea working. But the scrolling up, causes it to snap to the top. I need the sidebar to be fluid with the scrolling up/down motion. But sticking to the either the top or bottom of the window. It should also not be fixed positioned when the header/footer are in viewport, so it does not overlay.

Thanks

回答1:

See the fiddle here: http://jsfiddle.net/dKDJz/2/

Basically, you just watch for scroll events, and once the the window's scroll offset is greater than the top of the sidebar, it adds a fixed class to the sidebar, and removes when it's scrolled back up.

This was adapted from a solution posted on CSS-Tricks



回答2:

I have updated the jsfiddle http://jsfiddle.net/7ey9g/76/ with my solution.

The sidebar should remain in place at the top when the window is larger than the sidebar and fix to the bottom when the sidebar is larger.



回答3:

You don't need any jQuery or javascript for this. All of this can be achieved in CSS with position: fixed.

Change your sidebar selector to the following

#sidebar {
    width: 130px;
    height: auto;
    overflow: hidden;
    background: #ffffff;
    margin: 0 auto;
    clear: right;
    padding: 8px;
    background: #e5e5e5;
    border: 2px dashed red;
    position: fixed;
    right: 35px;
}


回答4:

This one may help

http://andrewhenderson.me/tutorial/jquery-sticky-sidebar/

http://designwoop.com/2011/09/tutorial-creating-a-sticky-sidebar-using-jquery/



回答5:

Unfortunately I don't have enough rep to simply comment with this link, so here ya go:

http://www.derekallard.com/blog/post/conditionally-sticky-sidebar

It's a few years old, but based off the screenshots it seems to cover exactly what you need.



回答6:

Try this here http://jsfiddle.net/dKDJz/8/

I've removed quite a bit of the code.

Is what I have done is that when you scroll down and it reaches the top of the sidebar it sticks to the top of the window. Once you scroll back to the top it will unstick if it meets the header no matter how tall the header becomes.



回答7:

I have played around with what Derek was doing, and I've accomplished the top and bottom being how you originally asked for.

I've edited some of the css for my needs, I'm sure you can just keep your old stuff in there and copy the script.

http://jsfiddle.net/mLdnb/3/

/* NewEdit */

I have replaced what I did with what you have. I came up with this:
http://jsfiddle.net/stillb4llin/dKDJz/47/

/* EndEdit */