Skip to content

Instantly share code, notes, and snippets.

@alexswider
Forked from ara303/tumblr-lightbox-for-photos.js
Last active September 8, 2015 20:58
Show Gist options
  • Save alexswider/1189cb5599549a9d5a2e to your computer and use it in GitHub Desktop.
Save alexswider/1189cb5599549a9d5a2e to your computer and use it in GitHub Desktop.
Demonstration of how to use Tumblr's built-in Lightbox() method to create a simple lightweight "presentation mode" for photo posts. This is advantageous over creating a whole new lightbox script because this code's already available (Tumblr use it for Photoset posts).
$(function(){ // shorthand document.ready()
$('.make_lightbox').each(function(){ // this is just an element I let them click, it carries a series of data- attributes.
$(this).on('click',function(){ // when clicked. this is the newer jQuery click() handler that's only in v1.8+ so that may be something to note.
var lbArray = []; // create blank array.
var arrayContents = {"width":$(this).data('width'), "height":$(this).data('height'), "low_res":$(this).data("lowres"), "high_res":$(this).data('highres')}; // make set of the data- attributes.
lbArray.push(arrayContents); // push the set into the array.
Tumblr.Lightbox.init(lbArray,1); // trigger Tumblr lightbox. the '1' is the number of photos it expects. specific to how tumblr has things set up.
$('body').toggleClass('tumblr_lightbox_active'); // use this to prevent the user from scrolling.
});
});
});
// Example usage:
{block:Photo}
{block:HighRes}
<div class="make_lightbox" data-width="{PhotoWidth-HighRes}" data-height="{PhotoHeight-HighRes}" data-lowres="{PhotoURL-500}" data-highres="{PhotoURL-HighRes}">View high resolution version</div>
{/block:HighRes}
{/block:Photo}
// and a bit of css to stop them from scrolling.
body.tumblr_lightbox_active {
overflow: hidden;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment