0
how can i get the Antepenultimate element in a div?
I am working with waypoints and i'm trying to load more elements when scroll is in the antepenultimate element, so when i load other div, i need to update the dipper (the element who detected when a div should be load) Any idea?
2 odpowiedzi
+ 6
Get the offset of the very last div, and load accordingly when the scroll offset reaches that. You can get them by class names, giving you an array of div's where it should be the last or length-1.
+ 3
<div id="wrapper">
<div></div>
<div></div>
<div></div>
<div></div> <!-- antepenultimate element -->
<div></div>
<div></div>
</div>
var list = document.getElementById("wrapper").getElementsByTagName("div");
var antep = list[list.length-3] // list.length-1 is index of last
You must adapt according to your element structure hierarchy:
<div id="wrapper">
<div></div>
<span></span>
<div></div>
<span></span> <!-- antepenultimate element -->
<div></div>
<span></span>
</div>
var list = document.getElementById("wrapper").getElementsByTagName("span");
var antep = list[list.length-2] // the antepenultimate element is the before last <span>