Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  61] [ 1]  / answers: 1 / hits: 9889  / 11 Years ago, tue, january 7, 2014, 12:00:00

I'm trying to get this infinite load script to work in my project.



Here's my HTML:



<!-- Contents -->
<div id=page-container>
<div id=scroller>
<div id=page_1 class=pagina></div>
<div id=page_2 class=pagina></div>
<div id=page_3 class=pagina></div>
<div id=page_4 class=pagina></div>
<div id=page_5 class=pagina></div>
<div id=page_6 class=pagina></div>
</div>
</div>

<div id=pages-to-load>
<div id=page_7 class=pagina></div>
...
<div id=page_25 class=pagina></div>
</div>


And here's the script:



function scrollalert(){
var pages = document.getElementById(scroller).getElementsByTagName(div);
var currentPageId = pages[pages.length - 1];
//console.log(currentPageId is: +currentPageId);
var scrollbox = document.querySelector('#page-container');
var scrolltop = $(window).scrollTop();
var scrollheight = scrollbox.scrollHeight;
var windowheight = $(window).height();
var scrolloffset=20;
console.log(scrolltop);
console.log(scrollheight);
console.log(windowheight);
console.log(scrollheight-(windowheight+scrolloffset));
if(scrolltop>=(scrollheight-(windowheight+scrolloffset))) {
//fetch new items
console.log(loading more pages);

(function() {
alert('test');
var i;
var pagesToLoad = $(#pages-to-load > div).size();
for (i = 0; i < pagesToLoad; i++) {
console.log(pagesToLoad[i].id);
$.get(pagesToLoad[i].id, function(newitems){
alert('get new page');
$('#scroller').append(newitems);
updatestatus();
})
};
})();
};
}


Whatever I try, I can't seem to load and append my new pages. Also when scrolling down, my scrollTop and scrollHeight don't change. I'm sure I'm missing something obvious here. Also my pages-to-load is undefined?


More From » jquery

 Answers
7

Here is one infinite-scroll script of mine using JQuery which works:



Html:



<html>
<head>
<title>Scroll Troll Page</title>
<script src=http://code.jquery.com/jquery-2.0.3.min.js></script>
</head>
<body>
<div id=scrollbox>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</body>




<script type=text/javascript>
$(window).scroll(function () {
//- 10 = desired pixel distance from the bottom of the page while scrolling)
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
var box = $(#scrollbox);
//Just append some content here
box.html(box.html() + <br /><br /><br /><br /><br /><br /><br />);
}
});
</script>


in Line:



box.html(box.html + Place content to expand here);


You can add the content that should be added to your container when reaching the bottom of the page while scrolling.



Working jsFiddle:



http://jsfiddle.net/MdrJ4/3/


[#48929] Monday, January 6, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rossthomasn

Total Points: 122
Total Questions: 78
Total Answers: 105

Location: South Georgia
Member since Sun, Aug 8, 2021
3 Years ago
rossthomasn questions
Wed, Mar 16, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Thu, Nov 26, 20, 00:00, 4 Years ago
Sun, May 31, 20, 00:00, 4 Years ago
;