Horizontal One-Page site won't go “backwards” to previous DIV

后端 未结 2 1993
被撕碎了的回忆
被撕碎了的回忆 2021-01-28 06:40

Quick backstory:

I had everything working perfectly, but after some time, I noticed it no longer functioned properly. The ability to go back a \"page\" is extremely impo

2条回答
  •  渐次进展
    2021-01-28 07:18

    This should fix your issue:

    DEMO

    $(document).ready(function () {
        $('.main-nav').on('click', function (e) {
            e.preventDefault();
            var toTarget = $(this).attr('href');
            history.pushState(null, null, toTarget);
            $(window).triggerHandler('hashchange');
        });
    });
    
    $(window).on('hashchange', function () {
        if(!window.location.hash) return;
        var $target = $(window.location.hash);
        $('html, body').stop().animate({
            scrollLeft: $target.offset().left,
            scrollTop: $target.offset().top
        }, 900, 'swing');
    });
    

提交回复
热议问题