如何查看屏幕外的元素 [英] How to see if an element in offscreen

查看:69
本文介绍了如何查看屏幕外的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆