最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to scroll table to particular tr programmatically - Stack Overflow

programmeradmin4浏览0评论

I want to scroll the html table to particular tr by using Javascript or jquery. Currently I can get the offset of the selected tr .And I am using the scrollTop method .I have tried the following but it is not working for me :

var table  = document.getElementById("table");
var tr = table.getElementsByTagName("tr")[3];
var scrollTo = tr.offsetTop;
table.scrollTop = scrollTo;

also I tried with jquery :

$('#table').animate({scrollTop:0},50);

can anybody help me where am I getting wrong ?

I want to scroll the html table to particular tr by using Javascript or jquery. Currently I can get the offset of the selected tr .And I am using the scrollTop method .I have tried the following but it is not working for me :

var table  = document.getElementById("table");
var tr = table.getElementsByTagName("tr")[3];
var scrollTo = tr.offsetTop;
table.scrollTop = scrollTo;

also I tried with jquery :

$('#table').animate({scrollTop:0},50);

can anybody help me where am I getting wrong ?

Share Improve this question edited Aug 3, 2016 at 12:08 sudo bangbang 28.2k11 gold badges77 silver badges77 bronze badges asked Apr 4, 2014 at 6:39 V-XtremeV-Xtreme 7,3339 gold badges40 silver badges80 bronze badges 2
  • please provide jsFiddle – Dipali Vasani Commented Apr 4, 2014 at 6:41
  • I have added all the code related to the functionality . – V-Xtreme Commented Apr 4, 2014 at 6:42
Add a comment  | 

3 Answers 3

Reset to default 19

This worked for me, try this

var elm = document.getElementById(id);
elm.scrollIntoView(true);

try this : http://jsfiddle.net/SZKJh/

var w = $(window);
var row = $('#tableid').find('tr').eq( line );

if (row.length){
    w.scrollTop( row.offset().top - (w.height()/2) );
}

reference :

https://stackoverflow.com/a/7853216/1982680

Here the simple step for scroll top function

 var s = $("table tbody > tr:nth-child(20)").position();
 $( "div" ).scrollTop( s.top );

Here the Demo

发布评论

评论列表(0)

  1. 暂无评论