2014-10-02 46 views
1

http://jsfiddle.net/3tpqnu1v/向上滾動的滾動表的顯着一行頂端

我有一個小桌子,標誌着一個特殊的一行,一類是「積極的」。 這怎麼可能,這顯着的行自動滾動到該表的頂部或底部或中心:

<table id="table" border="1"> 
    <thead> 
     <tr> 
      <th>h1</th> 
      <th>h2</th> 
      <th>h3</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr class="active"> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
       <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
     <tr> 
      <td>c1</td> 
      <td>c2</td> 
      <td>c3</td> 
     </tr> 
    </tbody> 
</table> 

回答

2

事情是這樣的,但我無法從該行獲得的偏移位置分毫不差。你需要玩弄正確的ypos。

http://jsfiddle.net/3tpqnu1v/2/

$('#scroll').on('click',function(e){ 
    var ypos = $('#table tr.active').offset().top - $("#table").offset().top - $('#table tr.active').height() ; 
    $('#table tbody').animate({ 
     scrollTop: $('#table tbody').scrollTop() + ypos 
    }, 500); 
}); 
+0

感謝這個工作,但我不需要動畫... – 2014-10-02 10:07:38