使用mCustomScrollbar jquery刷新后滚动到顶部如何防止它

时间:2014-03-27 06:16:18

标签: javascript jquery css

拖动滚动设置以滚动,以便用户可以滚动,但是当刷新发生时,滚动会返回到顶部。任何想法如何在页面刷新后保持滚动位置?

{literal}
        (function($){
            $(window).load(function(){
                /* custom scrollbar fn call */

                $(".content_2").mCustomScrollbar({
                    scrollInertia:150;,
                    theme:"light";
                });
            });
        })(jQuery);
{/literal}

您可以在此处查看图片enter image description here

1 个答案:

答案 0 :(得分:1)

在localstorage中设置它并在窗口加载时检索它:

$(window).load(function () {
    // load custom scrollbar here...
    var _top = localStorage.getItem('scrollPosition') || 0;
    $('html, body').scrollTop(_top);
});

$(window).on('scroll', function (e) {
    localStorage.setItem('scrollPosition', $(this).scrollTop());
});

演示http://jsfiddle.net/G5uFr/