如何查看屏幕外的元素 [英] How to see if an element in offscreen
问题描述
我有一个div列表,每当我想转到下一个div时,我都按一个键.我需要检查该div是否在屏幕外,如果是,我需要使用锚点或其他方法移动屏幕以显示该div.
I have a list of divs, and everytime I want to go to the next div I press a key. I need to check if that div is offscreen, and if so, I need to move the screen to show that div either using anchors or another method.
执行此操作的最佳选择是什么?
What is my best option for doing this?
为了澄清起见,在我看来,屏幕外表示某些内容如果不向下滚动就无法立即看到.因此,如果您位于顶部的StackOverflow主页上,则整个页面上的最后一个问题不在屏幕上.
Just to clairify, offscreen in my case means something that can't be seen right now without scrolling down. So if you are on the StackOverflow Home Page at the top, the last question on the entire page is offscreen.
推荐答案
最佳选择是通过获取y偏移量,检查窗口高度并计算滚动页面的位置来将页面滚动到元素页面到这一点.
Best option is to scroll your page to the element by getting its y-offset, and checking window height and calculating where to scroll page and then you can animate your page to that point.
//height of your div
var scroll = 250;
//animate from actual position to 250 px lower in 200 miliseconds
$(window).animate({"scrollTop": "+="+scroll+"px"}, 200);
所以这不是完整的代码,但可能会让您想到.
so this is not the complete code but it might give you the idea.
签出jquery scrollTop
希望它会有所帮助, 思南.
hope it helps, Sinan.
这篇关于如何查看屏幕外的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!