如果用Jquery实现在页面刷新时,让页面滚动到指定高度?

$(window).on('load',function(){
alert(1)
$(window).scrollTop(100);
console.log($(window).scrollTop())
})
这段代码,在页面刷新的时候,打印出来的scrollTop值也是100,这应该证明代码已经执行了,但是窗口的位置却没有变,求教大神,这是什么原因

我照你的方法试了试,是可以的。

你对照一下如下代码,看有啥错,改下jquery路径就可以运行


<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            html,body{
                margin: 0;
            }
            .d1{
                background: red;
                height: 100px;
            }
            .d2{
                background: green;
                height: 3000px;
            }
        </style>
        <script src="jquery-1.11.2.min.js"></script>
        <script>
            $(window).on('load',function(){
                alert(1);
                $(window).scrollTop(100);
                console.log($(window).scrollTop())
            });
        </script>
    </head>
    <body>
        <div class="d1"></div>
        <div class="d2"></div>
    </body>
</html>

温馨提示:答案为网友推荐,仅供参考