Giter VIP home page Giter VIP logo

infinite-scroll-with-isinviewport's Introduction

infinite-scroll-with-isinviewport

Simple Infinite Scroll uses jquery plugin isInViewport developed by zeusdeux to trigger load more action by determine the reach of the loading element with the plugin.

Demo

http://opensource.cbl-web.com/infinite-scroll-with-isinviewport

The example uses masonry for beautiful showcase, it is not necessary.

How it Work

Firstly, we need to listen the scroll event of $(window), by detect the .loadmore element is or is not in-viewport to decide do or not do the load more action.

Then we need to check it is during loading or not to prevent more then one load more action to be called.

Finally, call your loadMore function.

I would suggest you can save the load more ajax link, the page number and the max page number all in the .loadmore element by data attribute like the demo shown.

$container = $('#item-list');
$container.masonry();

$(window).scroll(function() {
	if(!$('.loadmore').hasClass('nomore') && $('.loadmore').is(":in-viewport")) {
		if(!$('.loadmore').hasClass('loading')) {
			loadMore($('.loadmore'));
		};
	};
});

function loadMore(el) {
	var nextPage = el.data('nextpage');
	el.addClass('loading');
	/*********** Do your ajax requrest to get more item ***********/
	setTimeout(function() { // Here I set a delay to show the loading effect more clear, you can remove this from your production
		$.post(el.data('url'),{page:nextPage},function(data) {
			$items = $(data); // Assume data is the html body of the items
			$container.append($items);
			$container.masonry('appended',$items);
			el.removeClass('loading'); // Remove the class "loading" from the .loadmore element to allow next load more event
			nextPage++;
			el.data('nextpage',nextPage); // Add up the page number and save back to the data attribute
			if(nextPage>el.data('maxpage')) { // Check if maxpage is reached
				/****** Do something to alert user no more item can load ******/
				el.addClass('nomore'); // This example I add a nomore class to .loadmore element and use css to show that no item is available
				/****** Do something to alert user no more item can load ******/
			}
		});
	},800);
	/*********** Do your ajax requrest to get more item ***********/
};

The html .loadmore element:

<div class="loadmore" data-nextpage="2" data-url="./ajax/path-to-load-more.html" data-maxpage="5">
  <div class="spinner-wrap">
    <div class="spinner"></div>
  </div>
</div>

infinite-scroll-with-isinviewport's People

Contributors

cambyliverson avatar

Watchers

James Cloos avatar gongmusian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.