So I know that $(window).scrollTop() will give me the current position of the browser scroll bar, but how do I find the total scrollable area?
i.e. If I scroll all the way down the final $(window).scrollTop() might equal 300px and scrolling back to the top would return it back to 0px, but suppose I wanted to find the total $(window).scrollTop() area before hand? How would I do that?
i.e. If I use $(window).scrollTop() I want something to happen when it get's to a certain point.
So pretty much is there a way to find out that $(window).scrollTop() total is 300px before hand? If the browser height is changed/resized then the $(window).scrollTop() will change, so I I'll need to find out what the new $(window).scrollTop() total is once the browser is resized.
Thanks.
You should use this to get the maximum scrolleable value:
$(document).height() - $(window).height()
You get the entire web height and substract the window height (the height that you can see on your window), so the rest of it is what is left to scroll
Related
I got this script
$("body, html").animate({scrollTop: $(document).height()}, 1000)
As seen above it will make so the the page is scrolled down to the documents height: aka the bottom.
However in my case its too far down, I would like to scroll down to bottom:100px; or bottom:10%;
Is this possible? I found examples but they didn't work for me.
What am I missing to make it work?:)
Sure, you can set the value of scrollTop to any value you can mathematically calculate:
// Top is 100 px short of bottom
$("body, html").animate({scrollTop: $(document).height() - 100}, 1000)
// 90% scroll down
$("body, html").animate({scrollTop: $(document).height() * 0.9}, 1000)
Note that for all these, 100px is very small. Your browser window would have to be <100px tall for it to NOT scroll all the way to the bottom.
You can use the visible height of the browser window in your equation too:
$(window).height()
https://jsfiddle.net/181zpvf2/
I am having some issues getting the <aside> element to stick to the bottom of the #main container when the user scrolls past it.
I have the <aside> element stick to the top once the user starts to scroll past it using the elements offset.top property, but since there is not a offset.bottom property, I am running into some issues figuring out how to get the math to work.
Any thoughts?
There's no offset().bottom but there's offset().top and height(). With that you get offset().bottom.
var offsetTop = $("aside").offset().top;
var offsetBottom = $("aside").offset().top + $("aside").height();
Now, with this you can do your math. Ask if you get stuck :-)
Use offset.top + window.innerHeight. This adds the window height to the offset of the screen so you get the position of the bottom of the screen.
var PositionFromTop = offset.top + window.innerHeight;
document.getElementById('asideElement').style.marginTop = PositionFromTop;
I love that this code works, but I cannot, for anything, wrap my head around WHY it's working?
Here is the jfidddle
Here is the code:
jQuery(document).ready(function($) {
clone = $('div').clone();
$('div').after(clone);
$('div:last').hide();
offset = $('div:first').offset();
var fromtop = offset.top;
$(document).scroll(function() {
doc = $(this);
dist = $(this).scrollTop();
if (dist >= fromtop) {
$('div:last').show();
$('div:first').css({
'position': 'fixed'
});
} else {
$('div:first').css({
'position': 'static'
});
$('div:last').hide();
}
});
});
I guess I am not understanding how scrolltop and offset are interacting or what they REALLY are, as in their true positions on the page. The code says if ScrollTop (the scrollbar position?) is higher than the value of the div's offsettop , then make the div sticky. But if ScrollTop is the position of the scrollbar, isn't it true that sometimes the scroll bar position could be lower than the div's position BEFORE the div is at the top of the page? What is it about being at the top of the page (offsettop of 0?)--and only at the top of the page, never before-- that makes offsettop a smaller value than scrolltop?
Really confused, and I don't want to just copy the code without understanding what it's really doing.
scroll Top is actually how many pixels 'up' the page has moved (or how many pixels you have moved down the page)
Basically all that happens is the .offset sees how far down the page (from the top of the page) the 'sticky' menu is
When you scroll to that point the bar becomes fixed (which is basically relative to the window instead of the document)
When you scroll back up it just switches back to being positioned in the document.
For clarity
.offset = 200px say - this is how far down the document the sticky menu is
.scrollTop - is 0 when the page loads
When you scroll down the page 201px
.scrollTop > .offSet -> so make the bar fixed (remember fixed is relative to the window - not the document)
If you scroll back up the process is reversed.
It's actually very simple. Let me try if I can make it a bit clear to you:
Whenever you want something (let's say some div) to get fixed on top as you scroll down, you need two things:
You need the current vertical position of your div. And you calculate that by using offset().top
You need to track how much user has scrolled. And you calculate that by using scrollTop()
So in your case, if the current position of your div is top: 100, then as soon as your scrollbar reaches the number 101, your div will get the class of .fixed
By default, the scrollbar vertical position is 0 when the page loads.
First of all,I would like to know the difference between these terms:
- $(window).height()
- $(document).height()
- $(window).scrollTop()
These terms look somewhat similar to me and I am unable to understand the clear difference between them. Here are my answers:
$(window).height() : Gives the height of window which a user can see.
$(document).height() : Gives total height of document. This can be more/less than window height depending upon the content on the page.
$(document).scrollTop() : gives the vertical position of scrollbar in window.
Real Question:
I am trying to implement lazy loading kinda thing where I need to make a call to server when scrollbar has crossed a point 200px from bottom of page. I am unable to use the above values to get this.
Any help would be appreciated.
The window is the area that you can see - as if your screen is a window and you are looking through at the document. The document is the entire document - it could be shorter or much longer than the window.
This is the math you need:
if( $(document).height() - $(document).scrollTop() < 200 ){
//Do something
}
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document
$(window).scrollTop(); //Get the current vertical position of the scroll bar for the first element in the set of matched elements or set the vertical position of the scroll bar for every matched element.
$(window).scrollHeight(); //Height of the scroll view of an element; it includes the element padding but not its margin.
Eventually, I figured out what should be the calculations after understanding these terms. Thanks to the answers. I was almost right in my definitions.
function (isScrollable) {
var isUserAtBottom = ($(window).height() + $(window).scrollTop() + 200 > $(document).height());
if (isUserAtBottom) {
// Do something (Like Ajax call to server to fetch new elements)
return true;
}
}
A view in my web app has a table which may be extremely long, so I wrapped it in a div with overflow: auto; max-height: 400px; so users can scroll through it while keeping the other controls on the page visible.
I want to use a bit of JavaScript to dynamically adjust the max-height CSS property so the div stretches to the bottom of the browser window. How can I determine this value? jQuery solutions are fine.
The table doesn't start at the top of the page, so I can't just set the height to 100%.
Something like this would work I think:
var topOfDiv = $('#divID').offset().top;
var bottomOfVisibleWindow = $(window).height();
$('#divID').css('max-height', bottomOfVisibleWindow - topOfDiv - 100);
I had a very similar problem, except in my case I had a dynamic pop-up element (a jQuery UI Multiselect widget), to which I wanted to apply a max-height so that it never went below the bottom of the page. Using offset().top on the target element wasn't enough, because that returns the x coordinate relative to the document, and not the vertical scroll-position of the page.
So if the user scrolls down the page, the offset().top won't provide an accurate description of where they are relative to the bottom of the window - you'll need to determine the scroll position of the page.
var scrollPosition = $('body').scrollTop();
var elementOffset = $('#element').offset().top;
var elementDistance = (elementOffset - scrollPosition);
var windowHeight = $(window).height();
$('#element').css({'max-height': windowHeight - elementDistance});
window.innerHeight gives you the visible height of the entire window. I did something almost identical recently so I'm pretty sure that's what you need. :) Let me know, though.
EDIT: You'll still need the Y-value of the overflowed div which you can get by document.getElementById("some_div_id").offsetHeight, seeing that .style.top won't give you a result unless it has been specifically set to a point via CSS. .offsetHeight should give you the correct 'top' value.
Then it's just a matter of setting the size of the table to the window height, minus the 'top' value of the div, minus whatever arbitrary wiggle room you want for other content.
something like max-height: 100%, but not to forget the html and body height 100%.