history pushState and scroll position

后端 未结 2 701
無奈伤痛
無奈伤痛 2021-02-19 04:44

I am trying to retrieve the scroll position when a user navigates back in the browser history using HTML5 popstate handler.

Here is what I have:

$(docume         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-19 05:36

    Find a way to make this more dynamic with jQuery, hope this can help :

    $(".lienDetail a").on('click',function(){
        history.pushState({scrollTop:document.body.scrollTop},document.title,document.location.pathname);
    }
    

    here the solution without jQuery :

    function pushHistory(e){
        e = e || window.event;
        var target;
        target = e.target || e.srcElement;
        if (target.className.match(/\blienDetail\b/)){
            history.pushState({scrollTop:document.body.scrollTop},document.title,document.location.pathname);
        }
    }
    
    if (document.body.addEventListener)
    {
            document.body.addEventListener('click',pushHistory,false);
    }
    else
    {
        document.body.attachEvent('onclick',pushHistory);
    }
    

    This will push history state for each click on link with lienDetail class for list of results for example

提交回复
热议问题