简体   繁体   English

跟踪 HTML div 内的滚动位置

[英]track a scroll position inside an HTML div

I am using google visualization table to create an html table, and it enables The header row remains fixed as the user scrolls.我正在使用google visualization table来创建一个html表,它使The header row remains fixed as the user scrolls.

I got something working like this: http://jsfiddle.net/RjHMH/114/我得到了这样的工作: http : //jsfiddle.net/RjHMH/114/

The problem I am facing is that, when I click a row to expand the table, it actually redraw the table, so that the scroll bar will always be at the top of the table.我面临的问题是,当我单击row以展开表格时,它实际上是重新绘制表格,因此scroll bar将始终位于表格的顶部。 Is there anyway I can track the current position of the scroll bar when I click, and set the value back when the table is redrawn?无论如何我可以在单击时跟踪滚动条的当前位置,并在重新绘制表格时将值设置回?

If it is a scroll bar of the DOM , I can use:如果它是DOMscroll bar ,我可以使用:

var pos = $('body').scrollTop();
table.draw(view, options);
window.scrollTo(0, pos);

Then how to track the scroll bar inside a div ?那么如何跟踪div内的滚动条呢?

var currentY = 0;

$(window).scroll(function () {
    currentY = $('selector').offset().top;
    console.log(currentY);
});

To get the position on click:要获得点击位置:

$(document).click(function () {
    currentY = $('selector').offset().top;
    console.log(currentY);

});
document.getElementsByClassName("google-visualization-table")[0].children[0].onscroll = function(){
    console.log(this.scrollTop); //check the number in console
}

The key is to get the div in js, or use jquery, then you can visit the scrollTop of that div.关键是在js中获取div,或者使用jquery,然后就可以访问那个div的scrollTop了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM