I have seen several solutions which determine when an element is visible in the viewport whilst scrolling the page, but I haven’t seen any which do this for elements that are contained in a scrolling container div as in the example here.
How would I detect the items as they scroll into view via the scrolling div? And by contrast how would I detect them if they fell out of view. In all cases the overflow elements are not hidden at the outset.
HTML
<div id="mainContainer" class="main">
<div id="scrollContainer"class="scroller">
<div id="picturesContainer"class="holder">
<div id="pictureContainer1" class="picture position1">
pictureContainer1</div>
<div id="pictureContainer2" class="picture position2">
pictureContainer2</div>
<div id="pictureContainer3" class="picture position3">
pictureContainer3</div>
<div id="pictureContainer4" class="picture position4">
pictureContainer4</div>
<div id="pictureContainer5" class="picture position5">
pictureContainer5</div>
<div id="pictureContainer6" class="picture position6">
pictureContainer6</div>
<div id="pictureContainer7" class="picture position7">
pictureContainer7</div>
<div id="pictureContainer8" class="picture position8">
pictureContainer8</div>
<div id="pictureContainer9" class="picture position9">
pictureContainer9</div>
</div>
</div>
</div>
CSS
.main{
position:absolute;
top:0px;
left:0px;
height: 200px;
width:200px;
background-color: grey;
border: 1px solid black;
}
.scroller{
position:absolute;
top:0px;
left:0px;
height: 250px;
width:250px;
background-color: lightblue;
border: 1px solid black;
overflow: scroll;
}
.picture{
position:absolute;
height: 200px;
width: 200px;
background-color: lightyellow;
border: 1px solid black;
}
.position1{
top:0px;
left:0px;
}
.position2{
top:0px;
left:200px;
}
.position3{
top:0px;
left:400px;
}
.position4{
top:200px;
left:0px;
}
.position5{
top:200px;
left:200px;
}
.position6{
top:200px;
left:400px;
}
.position7{
top:400px;
left:0px;
}
.position8{
top:400px;
left:200px;
}
.position9{
top:400px;
left:400px;
}
.holder{
position:absolute;
top:0px;
left:0px;
width:600px;
height:600px;
background-color:lightgreen;
}
include jQuery library on page first.
Add in HTML for results output:
It is code based on object’s coords, witch are recalculated on scroll event. There are some things to know. IE and, seems to be, Opera takes into consideration width and height of scrollbars themselves, that demands curtain code tuning. I just have suggested solution direction and did not spent much time for debugging this.
And yet, maybe will be useful following (from jquery api about offset):