Skip to content

Instantly share code, notes, and snippets.

@SaFrMo
Created June 21, 2016 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaFrMo/b0c336d1c1a8581470201a65f1efd27d to your computer and use it in GitHub Desktop.
Save SaFrMo/b0c336d1c1a8581470201a65f1efd27d to your computer and use it in GitHub Desktop.
Infinite scroll on Teller
// if not currently loading AND page has teaser AND page is scrolled to teaser area
var teaserHeight = 250;
if ( !jt2016.loadingPost && jQuery('.teaser').length && (jQuery('.teaser').offset().top < jt2016.sTop + jQuery(window).height() - teaserHeight) ) {
// get url from teaser element
var url = jQuery('.teaser').data('url');
// make new promise
jt2016.loadingPost = jQuery.ajax(url, {dataType: 'html'}).promise().done(function(data){
// parse new page
var $html = jQuery(data);
// get and set up pieces of page
var $bookingLink = $html.find('#fixed-header .booking-link');
var $entry = $html.find('#content .entry');
var $teaser = $html.find('.teaser');
// add entry to current page
jQuery('.teaser .meta').after($entry);
// turn teaser into incoming post, append new teaser
var $newPost = jQuery('.teaser')
.removeClass('teaser')
.attr('id', $html.find('#content .post').attr('id'))
.addClass('loaded-async')
.after($teaser);
// add horizontal bar after incoming post's Next Article divider
$newPost.find('.next-article').after('<div class="horizontal-line"></div>');
// focus on that bar and add class to animate
$newPost.find('.horizontal-line').focus().addClass('loaded');
// re-run some init functions
jt2016.onAjaxComplete();
// get html of new booking link
var bookingLinkHTML = $bookingLink.html() || '';
// push incoming state to cache
jt2016.blogPostCache.push({
top: $newPost.offset().top,
url: url,
bookingLink: bookingLinkHTML,
title: $html.filter('title').text()
});
// reset ajax promise
jt2016.loadingPost = false;
});
}
@drewbaker
Copy link

drewbaker commented Jun 21, 2016

This is what I ended up doing:

    initSingle: function(){

        jQuery(window).scroll(function(){

            var docHeight = jQuery(document).height();
            var scrollPosition = site.winHeight + site.sTop;
            var buffer = 200;

            if( (docHeight - scrollPosition) < buffer) {
                // At bottom!
                var $last = jQuery('.next-post').last();
                var url = $last.find('a').attr('href');
                    $last.remove();

                site.ajaxPost(url);
            }
        });

    },

    ajaxPost: function(url){

        // Abort if no URL passed in
        if(url == null) {
            return false;
        }

        // Get new post
        jQuery.ajax(url, {
            dataType: 'html'
        }).promise().done(function(data){


            // Append new post and fade in
            var $newPost = jQuery(data).find('.blog-post');
                $newPost.hide();

            jQuery('#content').append($newPost);
            $newPost.velocity('fadeIn');

            // Run some init functions again
            site.initBlogStyles();

            // Update browsers URL
            History.pushState({}, $newPost.data('title'), $newPost.data('url'));

        });

    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment