I know title is a bit confusing ;D but basically what I want to do is clearly demonstrated on this website http://9gag.com scroll down and pay attention to sidebar, there are 2 advertisements once 2nd advertisement reaches top of the page it starts scrolling down with a page.
I would like to know how to do this? html/css or jQuery solutions are prefared.
// To scroll to the bottom of a div const theElement = document. getElementById('elementID'); const scrollToBottom = (node) => { node. scrollTop = node. scrollHeight; } scrollToBottom(theElement); // The specified node scrolls to the bottom.
Use position:fixed; and set the top:0;left:0;right:0;height:100px; and you should be able to have it "stick" to the top of the page.
For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.
No, by far the best way to jump to the top or bottom of a Web page is by tapping your Home or End key, respectively. This works in Chrome, Firefox, and Internet Explorer (and probably every other browser as well–those are just the ones I've tested). Try it–I guarantee it'll join your list of favorite shortcuts.
As Kevin points out, what you need to do is to listen to the document's scroll
event. When that is fired, you'll want to look at the value of scrollTop
.
$(document).scroll(function() {
var useFixedSidebar = $(document).scrollTop() >= myThresholdValue;
$('.my-sidebar-items').toggleClass('fixed', useFixedSidebar);
});
And your CSS would basically look like this:
.my-sidebar-items.fixedSidebar { position: fixed; }
A complicating factor is that you'll probably want the items to be fixed in their new, top positions, and you would want to do something like this:
.my-sidebar-items.fixedSidebar { position: fixed; top: 0 }
However, that would probably stack your items on top of one another. A solution to that would be to put the fixedSidebar
class on a container, and use the class as described.
Demo
You could use sticky bars http://archive.plugins.jquery.com/plugin-tags/sticky or jQuery Waypoints http://imakewebthings.github.com/jquery-waypoints/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With