2013-01-10 192 views
0

我正在查找与类'.selected'的div是否低于其容器div的底部(被overflow:hidden隐藏)。如果是,请向下滚动容器的内容以显示下一个“页面”,方法是滚动容器高度的等效值或滚动,直到“.selected”再次位于容器的顶部。滚动,如果div被溢出隐藏

要做到这一点,最好的方法是什么?

+1

尝试jQuery的网站上查找scrollTo。我为Theakstons网站在线商店使用了类似的东西 – Pete

+0

@aaron我还没有尝试过任何东西,我想我会在跳入它之前询问这里。我发现这虽然http://stackoverflow.com/questions/5287425/overflow-hidden-jquery-selector这是一种相关 – ejfrancis

回答

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 =内容容器高度
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()