2016-03-04 100 views
0

我檢查了所有可能與此相關的問題,但我無法找到任何適合我的案例,或者答案不好或不完整。滾動到頁面底部,重新加載,然後重複

在我的情況下,我有一個html頁面,其中有一些php,顯示比賽結果。由於比賽現場進行,帶結果的頁面應滾動到頁面底部,然後頁面應刷新(以便可能的新分數進來),然後重複一遍。什麼是我的問題的最佳解決方案?

頁面大小/長度會隨着更多數據進入結果頁面上的表格而增加。

圖片:Site preview

編輯

此代碼現在滾動到頁面的底部,然後跳回頂部和重複及其正是我想要的,但我想一個頁面每當我點擊底部時refreshafter this去頂部。

$(function() { 


    var pageScan = { 
     speed : 10000, 
     loop : true, 
     delayRestart : 1000, 
     start : function(){ 
      pageHeight = $('body').height() - window.innerHeight; 
      pageScan.proc(pageHeight); 
     }, 
     proc : function(to){ 
      $("body").animate(
       {scrollTop: to}, 
       pageScan.speed, 
       "linear", 
       function(){ 
        if (pageScan.loop) { 
         setTimeout(function() { 
          window.scrollTo(0, 0); 
          pageScan.start(); 
         }, pageScan.delayRestart); 
        } 
      }); 
     } 
    }; 

    pageScan.start(); 

    }); 
+0

這是2016年現在使用AJAX ..角等etc. – Hardy

+0

因此,可以說你有一個高度900px高的頁面,你想滾動到底部,現在重新加載頁面和頁面的高度增加,讓說1300px現在你想繼續滾動從900到1300或從頂部開始即0-1300? –

+0

我想從頂部滾動到botom。@ Reddy – cufare

回答

0

您可以添加一些JavaScript到您的網頁:

<script> 
window.scrollTo(0,document.body.scrollHeight); 
setTimeout(function(){location.reload()},5000); 
</script> 
+0

此解決方案似乎不起作用。 @ danielenick89 – cufare

0

這可能是你在找什麼:

// Test data, ignore 
for(i = 1; i < 21; i++) { 
    $('#results').append('<p>Test Result No.' + i + '</p>'); 
} 

// Check every second (for example) if there is new data 
// In this example there is always data 
window.setInterval(function(){ 
    update_results(); 
}, 1000); 

// Check for new results 
function update_results() { 
    $.ajax({ 
    type: 'POST', 
    // Test data, ignore 
    data: { 
     html: 'new result' 
    }, 
    // your ajax.php file 
    url: '/echo/html/', 
    success: function(data) { 
     // Append new results to $('#results'); 
     // You might want to give back an array and loop through it with $.each(data, function() {}); 
     // you might also want to check for "false" if there are no new results 
     $('#results').append('<p>' + data + '</p>'); 

     // Scroll to bottom smoothly 
     $('html, body').animate({ scrollTop: $(document).height() }, 'slow'); 
    } 
    }); 
} 

jsfiddle

在你ajax.php-文件你可以呼應新的分數:

echo json_encode($scores); 

鑑於$分數是您可能從數據庫獲得的分數的數組。

+0

我不想以這種方式更新結果,我只是想刷新頁面,當我觸底。 @WcPc – cufare

相關問題