first enable the jQuery library on the page.
function getObjDims(obj){ if (!obj) return false; var off = $(obj).offset(); var t = { top:off.top, left:off.left, width:$(obj).width(), height:$(obj).height() }; return {x1:t.left, y1:t.top, x2:t.left+t.width,y2:t.top+t.height} }; function testInside(pic,box){ var d=getObjDims(pic); return (box.x1<=d.x1 && box.y1<=d.y1 && box.x2>=d.x2 && box.y2>=d.y2)?1:-1; }; $(document).ready(function(){ var inside={}; var port=$('#scrollContainer'); var box=getObjDims(port); $(window).resize(function(){ box=getObjDims(port); }); $(port).scroll(function(){ var str=[]; $('.picture').each(function(i){ var oldState = inside[this.id]!=undefined?inside[this.id]:0; var newState = testInside(this,box); inside[this.id]=newState; if (oldState!=newState) switch (newState){ case 1:str.push(this.id);break;
Add to HTML to output the results:
<div style='margin-top:280px;'>Pictures in window (numbers):</div> <div id='picStatus'></div>
This code based on object coordinates is recalculated for the scroll event. There is something you need to know. IE and Opera seem to take into account the width and height of the scroll bars themselves, which requires setting the curtain code. I just suggested a solution and did not spend much time debugging.
And yet, the following may be useful (from jquery api about offset):
Note. jQuery does not support getting the offset coordinates of hidden elements or accounting for borders, margins, or indents set on the body of an element.
Igor
source share