Created
November 1, 2012 00:16
-
-
Save joshsmith/3990801 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Infinite scroll */ | |
| // Init all the necessary items for infinite scroll | |
| var loading = false, | |
| handler = $('.to-masonify'), | |
| scrolled = false, | |
| $scrolltop = $('#scrolltop'); | |
| $scrolltop.bind('click', function() { | |
| $(window).scrollTop(0); | |
| $scrolltop.animate({ bottom: "-105px" }); | |
| scrolled = false; | |
| }); | |
| // Loop through all the .image's so we can dynamically | |
| // change their height, making it easier to do wookmark | |
| function changeImageContainers() { | |
| $('#bursts li').each(function() { | |
| var $image_container = $(this).find('.image'), | |
| $img = $(this).find('img'); | |
| var height = $img.attr('height'); | |
| $image_container.css( { height: height }); | |
| }); | |
| } | |
| // Init the wookmark options | |
| var wookmarkOptions = { | |
| container: $('#bursts'), | |
| offset: 20 | |
| }; | |
| changeImageContainers(); | |
| $(document).ready(new function() { | |
| handler.wookmark(wookmarkOptions); | |
| }); | |
| function showAndHideScrollLoader($loadingWrapper) { | |
| $loadingWrapper.show(); | |
| var counter = 2; | |
| function decrement() { | |
| if (--counter == 0) { | |
| $loadingWrapper.hide(); | |
| } | |
| } | |
| setTimeout(decrement, 1000); | |
| // Return the decrement so we can callback when AJAX load is complete | |
| return decrement; | |
| }; | |
| (function() { | |
| var page = 1, | |
| more = true; | |
| function nearBottomOfPage() { | |
| return $(window).scrollTop() > $(document).height() - $(window).height() - 400; | |
| } | |
| function scrolledDown() { | |
| return $(window).scrollTop() > 1000; | |
| } | |
| function getUrl() { | |
| var url = ""; | |
| if($('#bursts').hasClass('artworks-index-bursts')) { | |
| url = "artworks"; | |
| } else if ($('#bursts').hasClass('users-show-bursts')) { | |
| var username = $('#bursts').data('username'); | |
| url = username; | |
| } else { | |
| // There is no matching URL, probably don't want to load anything | |
| return false; | |
| } | |
| return url; | |
| } | |
| function animateScrolltop() { | |
| if(scrolledDown() && !scrolled) { | |
| $scrolltop.animate({ bottom: "-5px" }); | |
| scrolled = true; | |
| } else if(!scrolledDown() && scrolled) { | |
| $scrolltop.animate({ bottom: "-105px" }); | |
| scrolled = false; | |
| } | |
| } | |
| $(window).scroll(function(){ | |
| if (loading) { | |
| return; | |
| } | |
| animateScrolltop(); | |
| if(nearBottomOfPage() && more) { | |
| loading = true; | |
| page++; | |
| var url = getUrl(); | |
| console.log(url); | |
| if(!url) { | |
| return; | |
| } | |
| // Call the loading wrapper | |
| initLoaderHide = showAndHideScrollLoader($('#loading-more-wrapper')); | |
| $.ajax({ | |
| url: '/' + url + '?page=' + page + '&via=ajax', | |
| type: 'get', | |
| success: function(data) { | |
| loading = false; | |
| var $newElems = $(data).appendTo('ul.bursts').css({ opacity: 0 }); | |
| changeImageContainers(); | |
| if(handler) handler.wookmarkClear(); | |
| handler = $('.to-masonify'); | |
| handler.wookmark(wookmarkOptions); | |
| $newElems.animate({ opacity: 1 }, 'slow'); | |
| if($newElems.length == 0) more = false; | |
| }, | |
| error: function(data) { | |
| // An error occurred | |
| page--; // reset the page so we can call the AJAX again | |
| loading = false; | |
| }, | |
| complete: function() { | |
| initLoaderHide(); | |
| } | |
| }); | |
| } | |
| }); | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment