Auto load content on scroll
From UnionCloud Support
There are many pages on the platform that limit the content shown, and have a 'load more' button at the bottom which loads the next page when clicked.
The following code snippet enables automatic loading of the content as visitors scroll down the page, similar to how Facebook and Twitter do, improving the user experience as users can just scroll down without having to keep clicking 'load more'.
To snippet should be copied into Global Javascript.
1 <nowiki>
2 function EnableAutoLoadMore(buttonElement, contentElement) {
3 $(window).bind('scroll', function() {
4 if (typeof $(buttonElement).first().attr('data-loadmore') === typeof undefined || $(buttonElement).first().attr('data-loadmore') == false) {
5 $(buttonElement).first().attr('data-loadmore', 0);
6 }
7 if ($(buttonElement + ':visible').length > 0) {
8 if (($(window).height() + $(document).scrollTop()) > ($(buttonElement).first().offset().top - 80) && $(contentElement).first().children().length > $(buttonElement).first().attr('data-loadmore')) {
9 $(buttonElement).first().attr('data-loadmore', $(contentElement).first().children().length);
10 $(buttonElement).click();
11 }
12 }
13 });
14 }
15
16 $(function() {
17 if (window.location.pathname.substring(0, 7) == '/events' && window.location.pathname.length <= 8) {
18 EnableAutoLoadMore('#load-more-events', '#uc-event-right-panel-listing');
19 } else if (window.location.pathname.substring(0, 7) == '/groups') {
20 EnableAutoLoadMore('#uc-more-group-search', '.category-box-wrapper');
21 $('.club-navigation ul li').click(function() {
22 $('#uc-more-group-search').first().attr('data-loadmore', 0);
23 });
24 $('.group-types-wrapper ul li').click(function() {
25 $('#uc-more-group-search').first().attr('data-loadmore', 0);
26 });
27 } else if (window.location.pathname.substring(0, 9) == '/articles' && window.location.pathname.length <= 10) {
28 EnableAutoLoadMore('#load_more_article', '.uc-articles-listing');
29 } else if (window.location.pathname.substring(0, 16) == '/thestudentvoice' && window.location.pathname.length <= 17) {
30 EnableAutoLoadMore('#load_more_sv', '.uc-sv-left-panel-wrapper');
31 $('#uc-sv-navigation li a').click(function() {
32 $('#load_more_sv').first().attr('data-loadmore', 0);
33 });
34 }
35 });
36
37 $('html, body').bind('scroll mousedown DOMMouseScroll mousewheel keyup touchstart', function(){
38 $('html, body').stop();
39 });
40 </nowiki>