jQuery scrollTop not working on Mobile Devices (iPhone, iPad, Android Phones)
Today is a nasty day, the day when we discovered that our beloved $(selector).scrollTop(), a child method of $(selector).animate(), is not working properly on mobile devices.
On mobiles the javascript call simply blink on the top of the page, making our boss disappointed.
To tell you the truth it’s not your fault and this is why…
Mobile devices often doesn’t scroll the page moving the <html> or <body> elements, they move the viewport as far as i understood, we don’t really care, what we care is how make things shine again. Typically you’ll scrollTop or, better, animate with something like this:
$('html,body').animate( { scrollTop: 100, scrollLeft: 200 }, 800, function(){ $('html,body').clearQueue(); } );
which is fine, but as i said, it’s not working on mobile devices.
The solution? Here we are
if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)) { window.scrollTo(200,100) // first value for left offset, second value for top offset }else{ $('html,body').animate({ scrollTop: 100, scrollLeft: 200 }, 800, function(){ $('html,body').clearQueue(); }); }
or simply as suggested by Alex in the comments
The reason is because the mobile ios doesn’t know what $(‘html,body’) is you have to animate $(“body”) instead.
Thanks to the navigator.userAgent we know the device and discriminate the if-else. scrollTo can’t use fancy animations like jquery animate, but it does the job.
That’s all folks.
But, and the animation at mobile?
Removed, why?
Because you would find that most of the mobile devices actually have a really bad performance impact on javascript animations, the simplest transition would became laggy at the moment.
The reason?
Many, but the first one is phones and tables processor and their browser core coding, which is not optimized for this kind of things.
If you are running a mobile project you should suggest your clients to avoid using heavy animations on mobiles because of this, a laggy animations results in worse appeal than a simple blink.
I am here to help. The reason is because the mobile ios doesn’t know what $(‘html,body’)is. You have to animate $(“body”) instead.
Thank you a lot. Comment added to the post 😀
Hi Alex,
I try to animate body, it works but it flashes up and down for a few times until it jump to correct div. Have you experienced like this before?
If you are using an < a > element as the trigger try putting e.preventDefault().
With jquery usually $().click(function(e){ e.preventDefault(); //other stuff });
Thanks! This was helpful!
Thanks a million as this was a great help to me 🙂
Totally thank you for the save 🙂 By the way your blog header is amazing 🙂 why u not get website? 😀 😀
Because i merely make some posts during job breaks and my topics are not worth a site, but thank you for the compliments 🙂
Thanks. You saved my day. It’s very helpful.
My animate method is working fine in Ipad in all the html pages but in one page it is lagging and loading slowly, even in that page i don’t have big functionality.Here is my code please help me.
Your code is pretty plain, i don’t see many correction you can do. So my suggestion is this: jQuery animate on older iPads (1,2) can be pretty slow, it depends on the performance of the machine.
Going straight to the point i’d suggest you to use jQuery transit : http://ricostacruz.com/jquery.transit/
This library will make use of css animations, which are faster on mobile devices.
This is great. Though I just cant get it to work on my ipad mini, nor does it work on friends’ iPhones.
If you could look at my codepen and see if there is anything I can do…Ive been up all night trying to fix it haha
http://codepen.io/Spekachu/full/wKomgm
http://app.crossbrowsertesting.com/livetests/run/3748743
I want the same scrolling functionality if I can have it, but even the window.scrollto will not do anything on my ipad or iphones.
Sorry that I arrive so late, try to look at this! http://stackoverflow.com/questions/19929197/javascript-window-scrollto-issue-on-ipad
Good luck 🙂
Hi,
How do I use this for instafeed js plugin? I want my feed infinite scroll. Right now my code look like this. It works in desktop, but it doesnt work in mobile.
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
// call feed.next() once the page reaches the bottom
feed1.next();
feed2.next();
}
});
Nothing worked for me 🙁
I want to scroll o page with scrolltop which is inside an iframe.
http://www.jeanmichellsouber.com/iframe
If you are dealing with an iframe then the answer is
$('#myIframe').contents().scrollTop()
!Hi Jonathan! If you could please help, I would really appreciate it! I am developing an app in XDK and using Jquery to determine when the user scrolls to the bottom of first page of JSON data another page loads and more JSON data will appear. My problem is that scroll works fine in emulator and on tablet but not on the mobile phone. My code looks like this:
var currentPage = 1;
$(window).scroll(function () {
if ($(window).scrollTop() === $(document).height() – $(window).height()) {
currentPage++;
loadMoreAds(currentPage);
}
});
In app.css I see that by default I have:
body {
-webkit-touch-callout: none ;
-webkit-text-size-adjust: none ; -ms-text-size-adjust: none ;
-webkit-user-select: none ; -moz-user-select: none ; -ms-user-select: none ; user-select: none ;
-webkit-highlight: none ;
-webkit-tap-highlight-color: rgba(0,0,0,0) ;
-webkit-tap-highlight-color: transparent ; /* For some Androids */
}
html {
-ms-touch-action: pan-x ;
}
body {
-ms-touch-action: pan-y ;
-ms-content-zooming: none ;
}
I am just beginning my programming journey so I apologize if my question is stupid. Thank you!
try using
$(“body”).scroll();
$(“body”).scrollTop();
instead of $(window).
Also try to understand if it’s the .scroll listener that doesn’t work or the equation to determine if the user has scrolled!
Hi Jonath,
i have tried everything mentioned above to make scrolltop work in my IPad but unable to do it, my scenario is as below
application is rendering inside iframe where i want next button click should focus on top of next page.
Can you please help me for it or suggest something.
Try the solution proposed for this question: http://stackoverflow.com/questions/1192228/scrolling-an-iframe-with-javascript
You just saved my life
if(found){
filesize= file.size; //in bytes
totalFileSize += file.size;
if(totalFileSize > maxsize){
var value = Math.round((totalFileSize/(1024*1024))*100)/100;
//alert(‘You have exceeded the limit of size.. (‘+value+’ MB) \n Maximum limit of size you can use is 4.5MB’);
totalFileSize= totalFileSize – file.size;
if(isSinAttachment){
$(‘#ErrorMessage2’).show();
document.getElementById(“ErrorMessage2”).innerHTML = ‘Error: You have exceeded the limit of size.. (‘+value+’ MB). Maximum limit of size you can use is 1.0 MB.’;
}else{
$(‘#ErrorMessage2’).show();
document.getElementById(“ErrorMessage2”).innerHTML = ‘Error: You have exceeded the limit of size.. (‘+value+’ MB). Maximum limit of size you can use is 4.5 MB.’;
}
//window.scrollBy(0, -300);
if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)) {
alert(1111111111);
alert(navigator.userAgent);
console.log(document.body.scrollTop,1113333111);
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}else{
$(“html, body”).animate({ scrollTop: -1000 }, 600);
alert(navigator.userAgent);
}
// $(“html, body”).animate({ scrollTop: -1000 }, 600);
createTableJS();
}else{
document.getElementById(“ErrorMessage2″).innerHTML = ”;
getAsTextJS(file);
}
}else{
document.getElementById(“ErrorMessage2”).innerHTML = errmsg;
//window.scrollBy(0, -300);
$(“html, body”).animate({ scrollTop: -1000 }, 600);
createTableJS();
}
}
Thanks, this has worked!
Useful information. Fortunate me I found your website accidentally, and I’m shocked
why this accident did not took place earlier! I bookmarked it.