【问题标题】:Force page to load at top + Disable scrolling while page loads强制页面加载到顶部+页面加载时禁用滚动
【发布时间】:2014-02-18 06:22:16
【问题描述】:

我的标题有点小故障,如果页面加载到一半(如果在 URL 末尾带有 # 链接)或者在加载程序显示时滚动,它会中断。

如何使用 JS 在页面加载时禁用滚动,并强制页面加载到最顶部。

谢谢

【问题讨论】:

  • 你能分享一些代码吗?这样我们就可以调试了
  • 感谢您的帮助,决定废弃该网站并重新开始!

标签: javascript jquery html css load


【解决方案1】:

你可以试试这个:

$(document).ready(function(){
    $(this).scrollTop(0);
});

【讨论】:

  • 感谢您的帮助,非常感谢。我决定重新开始,所以没有什么乱七八糟的!看起来不错!
【解决方案2】:
// First you load the page at the top by adding a simple # at the end
$(document).ready(function() {
    var url = window.location.href;
    console.log(url);
    if( url.indexOf('#') < 0 ) {
        window.location.replace(url + "#");
    } else {
        window.location.replace(url);
    }
});
//Freeze page content scrolling while the page is loading
$(document).ready(function() {
    if($("html").css("position") != "fixed") {
        var top = $("html").scrollTop() ? $("html").scrollTop() : $("body").scrollTop();
        if(window.innerWidth > $("html").width()) {
            $("html").css("overflow-y", "scroll");
        }
        $("html").css({"width": "100%", "height": "100%", "position": "fixed", "top": -top});
    }
});
//Unfreeze page content scrolling when the page has finished loading
$(window).load(function() {
        if($("html").css("position") == "fixed") {
            $("html").css("position", "static");
            $("html, body").scrollTop(-parseInt($("html").css("top")));
            $("html").css({"position": "", "width": "", "height": "", "top": "", "overflow-y": ""});
        }
});

【讨论】:

    猜你喜欢
    • 2017-03-21
    • 2017-12-07
    • 2020-07-11
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多