Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fernandovalente

macrumors 6502
Original poster
I'm developing and website and I need it to scroll to an anchor. It needs to be animated, so I decided to use jQuery for that. I tried this:

Code:
function goToByScroll(id){
      $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}

It works perfectly on the desktop. On Mobile Safari it first goes to the top of the page, then it scrolls to the anchor I want it to scroll to. What's the trick to make it work on Mobile Safari?

Thanks in advanced
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
I created a script called Gentle Anchors a bit back. I don't know how it works on mobile Safari, but could be worth a shot.

I don't use jQuery so can't say if there's any issues with your syntax. I have seen others trying to do this though, such as this one, but hard to say how well they work on mobile Safari.
 

fernandovalente

macrumors 6502
Original poster
I created a script called Gentle Anchors a bit back. I don't know how it works on mobile Safari, but could be worth a shot.

I don't use jQuery so can't say if there's any issues with your syntax. I have seen others trying to do this though, such as this one, but hard to say how well they work on mobile Safari.

I got a question. How may I call the function to scroll to an anchor from javascript?
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
I got a question. How may I call the function to scroll to an anchor from javascript?

It wasn't really built for that, but I tweaked the code some to allow it since it didn't require much modification. The Gentle Anchors page has been updated to version 1.2. The example section on the page shows how to implement the new feature. I didn't do my normal testing across browsers, but I don't think there should be an issue.
 

lanativelea

macrumors newbie
Jul 29, 2013
1
0
Gentle Anchors ROCKS!

I just had to stop and thank Kendall Conrad for his amazing script that works so perfectly for iPhone Safari - I have tried many other solutions and they all came up short in one way or another. If you need to have precision scrolling to elements (and who doesn't when making a website work well in mobile) I highly recommend that you don't waste time as I did using other solutions. Here is the link again to Kendall's awesome script.

http://www.angelwatt.com/coding/gentle_anchors.php
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.