开发者

Find the DOM element containing a specific offset with jQuery or plain JavaScript

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜