Created
November 11, 2020 10:53
-
-
Save d-0-0I/4eb99054f426cc4fbd47dc5ffa90bfab to your computer and use it in GitHub Desktop.
Custom lightbox and infinite scrolling for Squarespace Jasper theme
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
<script> | |
// GALLERY // | |
function setUpImages() { | |
const imageSelector = ".thumb-image"; | |
$.fancybox.defaults.hash = false; | |
//wrap anchor tags around large images and add fancybox class | |
$(imageSelector).wrap(`<a class="fancybox" style="display:block"></a>`); | |
$(imageSelector).parent().attr("href", $(this).data('src')); | |
$(imageSelector).parent().attr("class", "fancybox"); | |
$(imageSelector).attr("data-fancybox", "gallery"); | |
$(imageSelector).fancybox({ | |
//this helper prevents jumping back to top of page after closing | |
padding: 0, | |
helpers: { | |
overlay: { | |
locked: false, | |
open: { | |
parent: "#content" | |
} | |
} | |
} | |
}); | |
} | |
window.Squarespace.onInitialize(Y, function(){ | |
setUpImages(); | |
}); | |
// INFINITE SCROLL // | |
function getAttributes ( $node ) { | |
var attrs = {}; | |
$.each( $node[0].attributes, function ( index, attribute ) { | |
attrs[attribute.name] = attribute.value; | |
} ); | |
return attrs; | |
} | |
function appendNewArticles() { | |
return "article" | |
} | |
var nextURL; | |
function updateNextURL( doc ){ | |
var nextSelector = $(doc).find('.BlogList-pagination-link span') | |
nextSelector.each( function(i) { | |
if ($(this).text() === "Older") { | |
nextURL = $(this).parent().attr('href'); | |
} | |
}) | |
} | |
updateNextURL(document); | |
function infiniteScroll() { | |
var infScroll = new InfiniteScroll( '.BlogList', { | |
path: function(){ | |
return nextURL | |
}, | |
append: appendNewArticles(), | |
debug: false, | |
prefill: true, | |
outlayer: false, | |
scrollThreshold: 1200, | |
hideNav: ".BlogList-pagination-link", | |
history: false | |
}) | |
infScroll.on('append', (response, path, items) => { | |
itemsArr = Array.from(items); | |
var style = $(".BlogList-item-image-link > img").first() | |
var attributes = getAttributes(style); | |
$(items).find('img').each((index, img) => { | |
$(img).attr("class", "foobar"); | |
img.src = $(img).attr("data-src"); | |
img.style = attributes.style | |
}); | |
var seen = {}; | |
$('article').each(function() { | |
var txt = $(this).attr('id'); | |
if (seen[txt]) | |
$(this).remove(); | |
else | |
seen[txt] = true; | |
}); | |
itemsCache = itemsArr; | |
}); | |
infScroll.on('load', function(response, path) { | |
updateNextURL(response) | |
}) | |
} | |
if (window.location.pathname === '/') { | |
infiniteScroll(); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment