Reputation: 23
I want to call a function when I'm scrolling html id called section-2
window.addEventListener("scroll", function() {
var elementTarget = document.getElementById("section-2");
if (window.scrollY > (elementTarget.offsetTop + elementTarget.offsetHeight)) {
alert("You've scrolled past the second div");
}
});
Upvotes: 0
Views: 221
Reputation: 330
Moz and its JS engine used in most non win browsers require parseInt() on many numeric properties returned from objects e.g. scrollY , Windows Jscript is kinder usually but it too carries a parseInt() and parseFloat() method. However, another feature is the correct "script" enclosing tags for the html or xml document can cause trouble too dependent the documents DTD version E.g. XHTML 1.0 or HTML 4.01.
Try this in the document head tag.
<script>
window.addEventListener("scroll", function() {
elementTarget = document.getElementById("section2");
if ((parseFloat(window.scrollY)) > ((parseFloat(elementTarget.offsetTop)) + (parseFloat(elementTarget.offsetHeight)))) {
alert("You've scrolled past the second div");
}
});
</script>
Upvotes: -1