Tyler Carter
Tyler Carter

Reputation: 61557

How to see if an element in offscreen

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?

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.

Upvotes: 1

Views: 2396

Answers (1)

Sinan
Sinan

Reputation: 11563

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.

check out jquery scrollTop

hope it helps, Sinan.

Upvotes: 2

Related Questions