15

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?

Peter Oram
  • 6,213
  • 2
  • 27
  • 40
  • Gotta love Safari Mobile zooming. $(window).scrollTop probably thinks it's at the top of the page because JS doesn't deal with zoom. You might try just moving the form down instead of scrolling the viewport up? – Matthew Blancarte Oct 24 '12 at 02:47

3 Answers3

1

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.

Zeno Popovici
  • 589
  • 3
  • 15
0

Did you try to use anchors?

They should work everywhere, because they are native function of browsers.

Original post with this code here

$(function() {
  $('a[href*="#"]:not([href="#"])').on('click', function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});
-2

No need to use jquery to do this:

window.scrollTo(0, 0);

You can also prevent the zooming all together using metatag:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

You may want to do this on just the page you are experiencing problems with?

George Filippakos
  • 16,359
  • 15
  • 81
  • 92