#Issue on mobile with smooth scroll functionality

6 messages · Page 1 of 1 (latest)

past osprey
#

Hi I am currently working on this page that has a back to top button https://355232a4eb.nxcli.net/board-development/ it uses an anchor tag but I also used this script to allow it to scroll to the top once you click on it. It seems to be working as expected on desktop but works incredibly inconsistently on mobile (i've tested both on my pixel and iphone). Not sure if anyone has any insight that might help or more info I can offer. Note the back to top button only shows up once you've scrolled down the page a bit.

This is the code

class ScrollToLink {
    
    constructor() {
        this.root = $('html, body');
        this.menu = $('.nav-wrapper');
        this.init();
        this.events();
    }

    events() {
        $('a[href^="/#"],a[href^="#"]').on('click', this.offsetAnchor.bind(this));
    }
    init() {
        if ($(location.hash).length != 0) {
            // let time = $(location.hash).offset().top - 50 > 4000 ? 2500 : 1500;
            this.root.animate({scrollTop:$(location.hash).offset().top - 50}, 0);
        }
    }
    offsetAnchor(event) {
        // this.menu.removeClass('active');
        document.documentElement.style.overflow = 'scroll';
        document.body.scroll = "yes";
        event.preventDefault();
        let link = $(event.currentTarget)[0];
        let href = link.hash;
        let time = 1500;
        
        if($(href) && $(href).offset() && $(href).offset().top) {
            $(href).offset().top - 50 > 4000 ? 2500 : 1500;


            this.root.animate({
                scrollTop: $(href).offset().top - 100
            }, time);
        }

        
    }

}

export default ScrollToLink;

The Oregon School Boards Association (OSBA) consists of over 1,400 members who serve 970,000 students statewide. Learn more about OSBA membership here.

feral widget
#

Good morning, Scott! I can't offer code advice on this, however I went to the site on my Samsung Galaxy s9 and the smooth scrolling works well, even when I expanded the more info list elements.

digital matrix
#

Is this whole piece of code to enable the back to top with smooth scrolling?
Have you tried the following CSS scroll-behavior: smooth on the <html> element to enable smooth scrolling for the whole page?

past osprey
#

it is to allow smooth scroll for any a tag that references an id on a page

#

scroll-behavior: smooth; added a delay to it only on desktop and didn't seem to make things different on mobile

digital matrix
#

Tested your site and The smooth effet when clicking the yellow button works well on safari mobile.