0

我正在寻找检测类 '.selected' 的 div 何时低于其容器 div 的底部(被溢出隐藏:隐藏)。如果是,则向下滚动容器的内容以显示下一个“页面”,方法是滚动容器高度的等效项,或者滚动直到“.selected”再次位于其容器的顶部。

这样做的最佳方法是什么?

4

2 回答 2

2

我使用scrollTo了我在评论中发布的链接中的一些数学来做到这一点:

var top = it.position().top; 
var bd = top + it.height();     
var ch = $('.tab-demo-content').height();     
if(bd>ch){  //if focused item isn't visible, scroll to it
    $(it).closest('.tv-container-vertical').scrollTo(it,500);   //this finds its parent container and scrolls it
}
  • bd=从容器顶部到所选项目底部的距离
  • ch= 内容容器高度
于 2013-01-10T21:38:19.597 回答
1

适当的本机(非 jquery)javascript 代码是:

element.scrollIntoView(); // Equivalent to element.scrollIntoView(true)
element.scrollIntoView(alignToTop); // Boolean arguments
element.scrollIntoView(scrollIntoViewOptions); // Object argument

(参见https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

大多数现代浏览器也支持 scrollIntoViewIfNeeded() :

element.scrollIntoViewIfNeeded()
于 2015-11-10T08:25:11.977 回答