Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollTop in mobile Safari not functioning

I have spent the last hour reading through the numerous questions on zooming and .scrollTop() and the variations of handlings and have tried at least 20 different things - everything that looked even remotely workable for my situation and I still have only a partial solution.

I have a form that has three fields and a button to continue. The continue button opens up the rest of the form. Safari mobile zooms in when you select the textfields. I don't mind that particularly. If I prevent the zoom then it can be too small to read the fields and if I load at a readable zoom level then you can only see part of the page, so the zooming in behaviour is ok.

The situation is that after the button is clicked and the other half of the form shows up, the window is still zoomed in and you're looking at a random patch of the page.

From what I can envision there could be two handlings:
1) Actually zooming out after clicking the button
2) Scrolling to where the next part of the form shows up

I tried everything I could find on zooming out and handling the viewport content width in the meta tag does fire, but doesn't handle it for me as it just makes the viewport bigger or smaller and doesn't actually zoom out at all. Potentially this works for others but not for me as I have a lot of styling already in place which may make that not an option.

I had settled for the scrolling solution and the .scrollLeft() is working fine, but .scrollTop() won't work not matter what selector I give it. I've tried:

$('body').scrollTop
$('html').scrollTop
$('document').scrollTop
$('body,html,document').scrollTop
$('html:not(:animated),body:not(:animated)').scrollTop
$('html:not(:animated),body:not(:animated),document:not(:animated)').scrollTop
$('#content').scrollTop // that's a wrapper div
$(window).scrollTop

Anybody know how to get .scrollTop() to work in Safari Mobile?

like image 659
Peter Oram Avatar asked Oct 24 '12 02:10

Peter Oram


People also ask

How do I scroll to the top in Safari?

If the address bar is hidden in Safari, you'll have to tap the status bar twice to return to the top.

What is $( window scrollTop ()?

scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0 .

How do you scroll up in Javascript?

The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost point.


1 Answers

I think you're trying to solve things in the wrong way. Just make sure your form fields have font-size: 16px on mobile viewports, this way Safari won't zoom in.

Also scrollTop may not work if you have position: fixed on your container.

like image 88
Zeno Popovici Avatar answered Oct 28 '22 20:10

Zeno Popovici