简体   繁体   中英

jQuery ScrollTo Chrome Fix

I've noticed that this doesn't seem to work in Chrome. I found a fix online (that works in the demo) but I can't get it to work for some reason:

Here is the code I have:

$('#button-top').bind('click', function(e) {
    try {
        e.preventDefault();
        target = this.hash;
        $('html, body').scrollTo(target, 150);
    } catch (error) {
        alert('error - ' + error);
    }
});

If it helps, here is a link to the site I'm building (see the link in the bottom right corner of the page):

http://www.mattpealing.co.uk/_dev/

You are loading an old version (1.4.2) of jquery.scrollTo . Change to the latest version 1.4.13 and it works, see the fiddle in the comments.

JQuery doesn't have any scrollTo() function. However, it does have a scrollTop(position) function.

This would work (i'm not sure what you meant with your 150):

 $('#button-top').bind('click', function(e) {
    try {
        e.preventDefault();
        target = this.hash;
        $('html, body').scrollTop(150);
    } catch (error) {
        alert('error - ' + error);
    }
});

If it's an animation you want, this will do the job (with an animation speed of 150ms):

$('#button-top').bind('click', function(e) {
    try {
        e.preventDefault();
        target = this.hash;
        $('html, body').animate({
            scrollTop: 0
        }, 150);
    } catch (error) {
        alert('error - ' + error);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM