2015-11-29 42 views
2

我想滾動div bottom到頁面的底部,當頁面加載時,但也想在需要時滾動備份,我怎樣才能存檔這個沒有任何動畫我怎樣才能做到這一點 echo "<div id='bottom'><p><b>$from_username</b><br />$message</p></div>";在頁面加載時滾動到底部,但將允許滾動頂部

<script> 

    function scrollpage() {  
     function f() 
     { 
      window.scrollTo(0,i); 
      if(status==0) { 
       i=i+40; 
       if(i>=Height){ status=1; } 
      } 
     setTimeout(f, 0.01); 
     }f(); 
    } 
    var Height=document.documentElement.scrollHeight; 
    var i=1,j=Height,status=0; 
    scrollpage(); 
    </script> 

這是我到目前爲止,我能夠自動向下滾動,但是當我想不能向上滾動,我能做些什麼,如果我想滾動up?

+1

以及你不斷調用它,因爲你的scrollpage();電話不包裹任何東西 –

+0

你是對的所以任何想法onhow我可以做的滾動爲特定股利 – jake123

+1

工作。給我5分鐘 –

回答

1

Scroll to bottom of Div on page load (jQuery)這是我找到我需要的代碼,把它放在一起給你。

<script> 
     $(document).ready(function() { 
      var d = $('#div1'); 
      d.scrollTop(d.prop("scrollHeight")); 
      console.log("at the bottom!"); 
     }); 
    </script> 
    </head> 
    <body> 
    <p>top</p> 
    <div style="max-height: 200px;"> 
    <div id='div1' style='background-color:grey;width:200px;overflow:scroll;overflow-x:hidden;max-height:200px;'> 
    <p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p></div> 
    </div> 
    <p>bottom</p> 
    <p>text under the div</p> 
    </body> 
</html> 
+0

我已編輯我的代碼,請現在看看它 – jake123