I wish to achieve content load when the user scrolls to the bottom of the page.
I am having a problem. It works fine on desktop browsers but not on mobile. I have implemented a dirty fix to make it work on the iPhone, but is not optimal as it won't work on other sized mobile devices.
My website is www.cristianrgreco.com, scroll down to see the effect in action
The problem is adding the scroll and the height do not equal the height on mobile devices, whereas they do on desktop browsers.
Is there a way to detect for mobile browsers?
Thanks in advance.
Below is the code currently being used:
$(document).ready(function () {
$(".main").hide().filter(":lt(3)").show();
if ($(document).height() == $(window).height()) {
// Populate screen with content
}
else if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i)) {
window.onscroll = function () {
if ($(document).height() - $(window).scrollTop() <= 1278) {
// At the moment only works on iPhone
}
}
}
else {
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() >= $(document).height()) {
// Works perfect for desktop browsers
}
})
}
})
scrollTop() + $(window). height() == $(document). height()) { alert("bottom!"); } }); You can test it here, this takes the top scroll of the window, so how much it's scrolled down, adds the height of the visible window and checks if that equals the height of the overall content ( document ).
You find the height of the scrolling container, then compare that to the scroll position. If they are the same, then you have reached the bottom.
To detect scroll end with JavaScript, we can listen to the scroll event. Then in the event listener, we check if offsetHeight + scrollTop is bigger than or equal to scrollHeight . We get the div with document. querySelector .
You can check if window. scrollY (the number of pixels the window has scrolled vertically) is equal to 0 . If you want to check if the window has been scrolled to its leftermost, you can check if window. scrollX (the number of pixels the window has scrolled horizontally) is equal to 0 .
For anyone who looks here later, I solved this by adding height=device-height
to the meta
viewport tag:
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0">
You can then carry on using $(document).scroll(function(){});
This worked with iOS 5
If you can, you should really avoid trying to hit an exact number.
Using Stelok's function above, you can test like this:
if ($win.height() + $win.scrollTop() > (getDocumentHeight()-10)) {
It's a bit softer - the user may not have quite scrolled to the exact last pixel but thinks he/she is at the bottom.
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