浏览器刷新时,Jquery,窗口会自动向下滚动

时间:2015-12-07 06:21:43

标签: jquery html css scroll

我将窗口设置为窗口的高度和宽度,使其看起来像一个"主屏幕"然后你可以向下滚动查看内容并且它工作正常,但是当浏览器刷新时它会自动向下滚动到你刷新之前的位置,但我希望它在" 0"指出文件,知道发生了什么事吗?

这是我的代码:

            <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script src="jquery/jquery-1.11.2.min.js"></script>
    </head>

    <style>
    html,body,p,h1,h2,div,section,img{
        margin:0;
        padding:0;

    }
    html,body{
    background:rgba(255,255,255,1.00);
    }
    header{
    background-color:rgba(181,255,0,0.51);
    width:100%;
    /* flex container */
    display: flex;
    flex-flow: row no-wrap;
    justify-content: center;
    }
    section{
        text-align:center;
    }

    </style>
    <body>
    <header></header>
    <section>
    <h2>Text</h2>
    <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
    </section>

    </body>
    <script>
        $(document).ready(function(){
            $(window).scroll(function(){
                console.log($(window).scrollTop());
                });

            //screen size
            var windowh = $(window).height();
            var windoww = $(window).width();
            //header full screen
            console.log(windowh + ' '+windoww);
            $('header').css({
                'height': windowh,  
            });

        });





    </script>
    </html>

2 个答案:

答案 0 :(得分:0)

我认为这与缓存或Cookie有关,但我相信这可能会让网站无法做到这一点......

$(window).on('beforeunload', function(){
    $(window).scrollTop(0);
});

答案 1 :(得分:0)

你可以试试这个:

$(document).ready(function(){
        $(this).scrollTop(0);
        $('html').animate({scrollTop:0}, 1);
        $('body').animate({scrollTop:0}, 1);
    });