1

I have linked to jQuery like so <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> and included the following script:

$(document).ready(function(){
    $('.myLinkToTop').click(function () {
        $('html, body').animate({scrollTop:$(document).height()}, 'slow');
        return false;
    });

    $('.myMenuLink').click(function () {
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });
})

But the script will only work in Firefox, and not Opera, Chrome or Safari. Is there anything I'm missing out?

Joe
  • 15,205
  • 8
  • 49
  • 56
Fin Moorhouse
  • 335
  • 5
  • 15
  • 2
    The WebKit engine doesn't like it when you mess with scrolling directly on the body element. (Doing it on *both* the body and the document seems pretty iffy anyway.) – Pointy Sep 02 '13 at 18:54
  • See [this old question of mine](http://stackoverflow.com/questions/1830080/jquery-scrolltop-doesnt-seem-to-work-in-safari-or-chrome-windows). – Pointy Sep 02 '13 at 18:54
  • @Pointy thank you, I think this is the issue. Any suggestions as to how to get around it? – Fin Moorhouse Sep 02 '13 at 18:55
  • Well what I ended up doing was wrapping all the relevant content in another block element (`
    `, `
    `, whatever), and use that as the thing to scroll. Seems ugly, but there you go.
    – Pointy Sep 02 '13 at 18:56
  • @Pointy I tried this and it didn't work. Thank you for your suggestion, though. – Fin Moorhouse Sep 02 '13 at 18:59
  • Using `$('html, body')` is the preferred cross-browser selector when animating the scrollTop, and it should work in all browsers, I've done it hundreds of times without issues, so something else is clearly wrong ? – adeneo Sep 02 '13 at 19:03
  • @FinMoorhouse Better you go to chrome, press F12, then go to console.. see what error it shows.. If its script error, then definitely it shows.. – Ganesh Babu Sep 02 '13 at 19:05
  • 1
    Seems to be working fine here -> http://jsfiddle.net/KJDhv/ – adeneo Sep 02 '13 at 19:08
  • Check your markup, make that html/body is your scrollable container. You can also check each scrollTop individually to see whats going on. Load your page, scroll down a bit and type $("body").scrollTop() into the console, then try $("html") etc. – ginman Sep 02 '13 at 20:39

0 Answers0