2015-12-07 153 views
0

我为窗口的高度和宽度设置了一个div,所以它看起来像一个“主屏幕”,然后你可以向下滚动查看内容,它工作正常,但是当浏览器刷新它会自动滚动到刷新前的位置,但我希望它在文档的“0”点,任何想法是怎么回事?jquery,当浏览器刷新时窗口自动向下滚动

这里是我的代码:

  <!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> 
+0

我不知道是不是这种情况,但是这可能发生在ASP.NET时MaintainScrollPositionOnPostBack prope rty设置为true https://msdn.microsoft.com/en-us/library/system.web.ui.page.maintainscrollpositiononpostback(v=vs.110).aspx –

回答

0

我认为这是与高速缓存或Cookie,但我相信这会保留网站做那......

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

你可以试试这个:

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