Reputation: 560
Please tell me how can I execute the function when the document scroll
is equal to the position of the #footer
element?
$(document).on('scroll', function() {
var s = $(this).scrollTop();
var scroll_footer = $('#footer').position().top;
$("#console").text(s + " " + scroll_footer);
if (s >= scroll_footer) {
console.log("load");
}
});
#block {
background: red;
width: 250px;
}
#child_block {
padding-top: 100px;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.child {
width: 100px;
height: 100px;
background: blue;
margin: 15px 10px 10px;
15px;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
}
#footer {
width: 100%;
height: 10px;
background: gray;
}
#console {
position: fixed;
left: 300px;
top: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="block">
<div id="child_block">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
<div class="child">7</div>
<div class="child">8</div>
</div>
<div id="footer"></div>
</div>
<div id="console"></div>
Upvotes: 0
Views: 115
Reputation: 469
I added a pageHeight
variable to your JS, so you're now calculating the window height in pixels + the amount the client has scrolled. When these two values >= scroll_footer
, your function can execute.
$(document).on('scroll', function() {
var s = $(this).scrollTop();
var pageHeight = window.innerHeight
var scroll_footer = $('#footer').position().top;
$("#console").text((s+pageHeight) + " " + scroll_footer);
if (s+pageHeight >= scroll_footer) {
console.log("load");
}
});
Here's a working codepen: https://codepen.io/paulmartin91/pen/dyNwJmp?editors=1111
Upvotes: 1