2016-07-31 10 views
2

スクロールがdiv要素

<script type="text/javascript"> 
 

 
    $(document).ready(function(){ 
 
     refreshTable(); 
 
    }); 
 

 
    function refreshTable(){ 
 
     $('#mainDiv').load('home.php', function(){ 
 
      setTimeout(refreshTable, 5000); 
 
     }); 
 
    } 
 
</script>

こんにちはでトップに、私はmainDiv内2.phpから5秒ごとに内容をロード1.phpでこのコード/スクリプトを持っています。今度は、新しいデータがdivの中の2.phpから読み込まれるたびに、mainDivを上にスクロールします。私にコード/スクリプトを表示してこれを達成する方法を教えてください。ありがとう。

+0

以下のようにあなたは、単純なコードを使用することができ、CSS( "トップ"、0);' – Thinker

+0

上記のコード内のどの行を挿入する必要がありますか? – kkambi

+0

settimeout関数の後に追加 – Thinker

答えて

0

divをスクロールするには、次のコードを使用します。

$('html, body').animate({ 
    scrollTop: $('#mainDiv').offset().top 
}, 'slow'); 

理解を深めるために以下のスニペットを確認してください。 div内にダミーデータを入力したので、スクロールしたdivのアイデアを得ることができます。

$(document).ready(function(){ 
 
    refreshTable(); 
 
}); 
 

 
function refreshTable(){ 
 
    $('#mainDiv').load('test.php', function(){   
 
     $('html, body').animate({ 
 
      scrollTop: $('#mainDiv').offset().top 
 
     }, 'slow'); 
 
     setTimeout(refreshTable, 5000); 
 
    }); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="mainDiv"> 
 
    
 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
Why do we use it? 
 

 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 
</div>

+0

ありがとうございますが、運がまだありません。これを行うには他の方法はありますか?私は自分のコードで何かを逃したように見えます。私のために私を識別できますか? – kkambi

+0

コードにデモを適用しましたか?あなたは仕事を分かち合うことができますか?だから私は識別することができます。 –

+0

はい私は適用されましたが動作しませんでした。どのように私はあなたが見るためにここに自分のコードを投稿することができます。 – kkambi

2

ちょうど `$( "#mainDiv")を使用

$('html,body').animate(
{ scrollTop: $("#div_id").offset().top - 30}, 'slow'); 
+0

ありがとうございますが、運がまだありません。これを行うには他の方法はありますか? – kkambi

関連する問題