views:

38

answers:

1

Hi,

I am paging an HTML page. In order to compute the page break offsets more efficiently, I was wondering if it is possible to get the element containing a certain coordinate offset from the beginning of the page.

Thanks a lot in advance for your help, Cheers!

+1  A: 

You could do something like:

var offset = 100; 
$(document.body).find('[offsetTop = '+offset']');

or

var offset = 100;
$("body *").filter(function () {
   return this.offsetTop == offset;
});

since you said they're all top level elements, the following should also work:

$(document.body).children().filter(function () {
   return this.offsetTop == offset;
});

You can also $(this).offset() to get the offsets. Using the offsetTop property only gets the offset from the parent I believe. But it may not matter since they are all top level elements.

CD Sanchez
Thanks Daniel,I guess I could do this.offsetTop >= offset to find the first element after a certain offset, right?
Engin Kurutepe
@Engin Kurutepe: Well, it will return all of the elements that match that condition, not just the first. But essentially, yes - you can put any condition there. Also, I'm not sure if they will be returned in order (according to the offset), so you should get that out so you aren't using the first element in the jQuery array assuming it's the first element that satisfies that condition (although it may well be).
CD Sanchez