Pardon me: Hello. I hope I am not confusing you guys in the title of my question.
Background: What I want is actually to update the content of the site. I was told about some plugins like infinite-scroll
throughout Google as well as Stackoverflow, but I don't want to use any plugins for that. What I found then was a pure jQuery code to detect the user's current position on the document. That goes something like this:
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
alert("bottom!");
}
});
What I found:
I found this code here: Check if a user has scrolled to the bottom
So that means that I have already read all the questions on stackoverflow.
What is the issue: The issue is that the code above doesn't work fair. It works good only if I am using: == $(document).height()
because it will just run the code when the user is at the end of the document. Once I try to use something like: $(document).height() - 100
or anything else like that. It doesn't work. I have to go to end of the document, and when I come back it shows the alert! That would be pretty unlikely to the user.
I have also tried the unbind('scroll')
but that stops the code execution once its gone through an alert! I mean it just alerts the user once, so indirectly it will load the data for the user once, and will stop loading more data again.
So I thought asking the question here would be a better idea. Can I get a simple jQuery code, which would come to know about the user's presence on the screen! And lot the data once, because when I use > $(document).height()
it keeps showing me the popup, but it should stop showing popup and start showing pop ups again once the request has been made!
Or in simple words:
Or in other words, what I want is that the request should be made when the user has gone 80% down the way to the bottom so when he reaches almost 95%th part of the page the data is placed there and he can view it, and then again when he reaches 80%th part down request is made. And so on! This part would be easy to understand.
Using this works:
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
alert("Sending ajax request!");
$.ajax({
url: "ajax_requests/send_email_digest",
success: function (data) {
// show the data..if any
}
});
}
});
But this doesn't:
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() ==
$(document).height() - 100) {
alert("Sending ajax request!");
$.ajax({
url: "ajax_requests/send_email_digest",
success: function (data) {
// show the data..if any
}
});
}
});