Rechecking something in jQuery?
Basically I have a jQuery function that checks if the user is near the bottom of the page and loads more stuff. When the document loads 开发者_StackOverflow社区this is ran, to check.
<script type="text/javascript">
$(document).ready(function() {
var number = 5;
offset = 0;
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 10) {
$(".empty-div").append().load('<?php bloginfo('template_url'); ?>/ajax.php?offset='+offset+number, function() {
var offset = offset+number;
setTimeout(function(){ console.log('after', $(document).height()); }, 0);
setTimeout(function(){ console.log('after', $(window).height()); }, 0);
});
}
});
});
</script>
H/e whenever I reach the bottom of the page on the second go, it doesn't redo the load function. It does work the first time, by the way. Any ideas how to remedy this? The console stuff is there to recalculate the heights, just in case that's what's causing it.
By doing this:
var offset = offset+number;
You're creating a new local offset
variable, not updating your original
offset = 0;
declared before the function, so this: ?offset='+offset+number
is always using offset
as 0
, since it's never updated. Just remove the var
, like this:
offset = offset+number;
For cleanliness, also add a var
to the original declaration, or use a comma for a multiple declaration, like this:
var number = 5, offset = 0;
you use load() there on $(".empty-div"), this would'nt add more content, it will replace the content(if the response does'nt change, you will see no change)
It better should be something like
$('<div/>').appendTo(".empty-div").load(/*****/)
maybe there also is a problem with the DOCTYPE. Log
$(window).scrollTop(), $(window).height() and $(document).height()
maybe, it differs between standards - and quirks-mode.
精彩评论