Created
May 30, 2019 13:46
-
-
Save ronipl/de6102bbfde37a337457eb338457bb30 to your computer and use it in GitHub Desktop.
Shopify infinite scroll load more
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
// Shortcircuit variable | |
let triggered = false; | |
function ScrollExecute() { | |
// Locate loadmore button | |
let moreButon = $('#more').last(); | |
// Get URL from the loadmore button | |
let nextUrl = $(moreButon).find('a').attr("href"); | |
console.log(nextUrl); | |
// Button position when AJAX call should be made one time | |
if ((($(moreButon).offset().top - $(window).scrollTop()) < 800) && (triggered == false)) { | |
console.log(nextUrl); | |
// Trigger shortcircuit to ensure AJAX only fires once | |
triggered = true; | |
// Make ajax call to next page for load more data | |
$.ajax({ | |
url: nextUrl, | |
type: 'GET', | |
beforeSend: function() { | |
moreButon.remove(); | |
} | |
}) | |
.done(function(data) { | |
// Append data | |
$('.collection-product-items').append($(data).find('.collection-product-items').html()); | |
// On success, reset shortcircuit | |
triggered = false; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment