Popstate - double click on the back button to actually return

I create a single page page and use pushState to change the address. Now, if I pull back, the popup starts up, and I want to animate the page scroll from the current position to the last position. This works, of course, but the page jumps up.

Does anyone know how to prevent this behavior? I am using jQuery to animate scroll ...

Thanks:)

// edit: ok I found out that if I animate the scroll and then change the URL, it won’t jump - but now I need to press the back button twice to get back ...
http: //www.testwebseiten .at / user / falk / stein-jagersbacher.at

The code is a bit dirty, so I explain it a bit:

    var scrollCurrentSection = function(event) {
        if (typeof event.preventDefault != 'undefined')
            event.preventDefault();

        var page = "";
        if (window.location.href.substr(-1) == '/')
            page = sjag.index;
        else
            page = window.location.href.match(/([^\/]+)$/)[1];

        $this       = $('a[href$="'+page+'"]');
        if ($this.length) {
            sjag.scrollToSection($this);
        }
    }
    window.onpopstate = scrollCurrentSection;

sjag ... sjag.index "index.php", sjag.scrollToSection .

:

    // set navi
    $("#navi a, #navi-add a").click(function(event) {
        var data = $(this).data('sjagpage');
        if (typeof data == 'undefined')
            return;

        event.preventDefault();
        // animate scrolling
        $this   = $(this);
        sjag.scrollToSection($(this), function() {
            history.pushState("", "", $this.attr('href'));
        });
    });

$(this).data('sjagpage') jQuery.

, , - , ... ?

+3
1

, , event.preventDefault - , event jQuery, jQuery. , 2 , .

:

window.onpopstate = scrollCurrentSection;

- :

$(window).bind("popstate", scrollCurrentSection);
0

Source: https://habr.com/ru/post/1626547/


All Articles