Scroll to anchor on Mobile Safari

Discussion in 'Web Design and Development' started by fernandovalente, May 25, 2010.

  1. macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #1
    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
     
  2. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    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.
     
  3. thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #3
    Thank you so much man! It's a pretty good script. It works perfectly on Mobile Safari. YOU SAVED MY LIFE!!! Thanks a lot for this GREAT script!
     
  4. thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #4
    I got a question. How may I call the function to scroll to an anchor from javascript?
     
  5. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    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.
     
  6. macrumors newbie

    Joined:
    Jul 29, 2013
    #6
    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
     

Share This Page