Tumblr Lightboxes
There is a way to enable native Tumblr lightboxes for the photosets, but be aware that this will require JavaScript. The setup is also a little bit different.
1. (this step isn't different from before) under
<head>
, paste
<link href="https://static.tumblr.com/0podkko/oDSpg7y88/photosets.css" rel="stylesheet">
2. replace your photosets block (e.g.
{Photoset}
or pxu's photoset) with
<div class="photoset-grid" photoset-layout="{PhotosetLayout}">{block:Photos}<div data-width="{PhotoWidth-HighRes}" data-height="{PhotoHeight-HighRes}" data-lowres="{PhotoURL-500}" data-highres="{PhotoURL-HighRes}" onclick="lightbox(this)"><img src="{PhotoURL-HighRes}" /></div>{/block:Photos}</div>
3. either before
</head>
or right before
</body>
(basically wherever you add your scripts), paste
<script>
function gatherData(images, arr) {
for (let i = 0; i < images.length; i++) {
let currentData = {
"width": images[i].getAttribute('data-width'),
"height": images[i].getAttribute('data-height'),
"low_res": images[i].getAttribute('data-lowres'),
"high_res": images[i].getAttribute('data-highres')
};
arr.push(currentData);
}
}
function getIndex(elem) {
let i = 0;
while( (elem = elem.previousElementSibling) != null ) i++;
return i;
}
function lightbox(elem) {
let currentPhotoset = elem.parentNode;
let photosetPhotos = currentPhotoset.getElementsByTagName('div');
let data = [];
gatherData(photosetPhotos, data);
Tumblr.Lightbox.init(data, getIndex(elem) + 1);
}
</script>
You do not need to add jQuery if you don't already have it included. If you already have scripts in your code, you can simply add the functions in your existing
<script>...</script>
. If you are using infinite scroll, there is no function that you need to add in your callback function.
The CSS selectors and styling are the same as above. The photoset structure is also the same.
I would recommend adding this style, to ensure that users know that the photoset has a clickable lightbox:
[photoset-layout] div {
cursor: pointer;
}
lightboxinstructionsinstall
The JavaScript-Modified Version
A modified version of photosets.css using JavaScript has been created to reduce the file size and quicken load times. The photosets are still styled entirely with CSS, but the file size of the CSS (and JavaScript) files are much smaller. Some JavaScript knowledge is recommended, especially if you're interested in using it past it basic capabilities.
How to Install
1. under <head>
, paste
<link href="https://static.tumblr.com/0podkko/xIbplezmo/modified_photosets.css" rel="stylesheet">
2. replace your photosets block (e.g. {Photoset}
or pxu's photoset) with...
for just the photosets: <div class="photoset-grid" photoset-layout="{PhotosetLayout}">{block:Photos}<div><img src="{PhotoURL-HighRes}" /></div>{/block:Photos}</div>
for photosets with lightboxes: <div class="photoset-grid" photoset-layout="{PhotosetLayout}">{block:Photos}<div data-width="{PhotoWidth-HighRes}" data-height="{PhotoHeight-HighRes}" data-lowres="{PhotoURL-500}" data-highres="{PhotoURL-HighRes}" onclick="lightbox(this)"><img src="{PhotoURL-HighRes}" /></div>{/block:Photos}</div>
3. either before </head>
or right before </body>
(basically wherever you add your scripts), paste <script src="https://static.tumblr.com/0podkko/jfMplm3f8/modified_photosets.js"></script>
4. right after the pasted <script>
link, paste <script>initPhotosets();</script>
JQuery is not needed for the script to run properly. If you already have a <script>
with JavaScript, you can simply call initPhotosets();
within it.
You do not need to add any additional JavaScript for native Tumblr lightboxes; the code is included with the Javascript file.
If you are using PXU (or other JavaScript-based) photosets, please remember to remove any code and scripts relating to them when installing these photosets!
Additional Functionality and Notes
Structure
After initPhotosets();
runs, the photosets will have a structure like the CSS-only photosets. The only difference is that the photo containers will have an additional attribute of index="#"
. The index attribute represents what number photo each photo is. (This attribute may be helpful to those who would like to use custom photoset lightbox code.)
Callback Functionality
The initPhotosets() function has the ability to execute a callback function. A callback function in this case is a function that runs after all the photosets on the page have been initialized/styled. This is useful to those who have code that needs to run after all the photosets are ready.
To use this functionality, simply place the function between the parentheses. (For those who are familiar with JavaScript, you are passing the function as an argument.)
initPhotosets(function(){
// add your code here
});
Alternatively, if you're calling an already declared function, you can pass it like so:
initPhotosets(functionName());
This functionality may be useful for those who wish to call their own lightbox code, instead of using Tumblr's native lightbox.
Infinite Scroll
If you are using infinite scroll, you must call the function again in the infinite scroll function's callback function.
If you are using Paul Irish’s infinite scroll code, the callback function is the function that is at the end of all the options.
$container.infinitescroll({
// infinite scroll options
}, function(items) {
initPhotosets(); // if you used a callback function for this before, you must use it again here; if you do not know what a callback is, see above subheading
// your other code
});
If you are using David DeSandro’s infinite scroll code, the “callback function” is the function within container.on( 'append.infiniteScroll', function( … ) { code });
.
$container.on( 'append.infiniteScroll', function( event, response, path, items ) {
initPhotosets(); // if you used a callback function for this before, you must use it again here; if you do not know what a callback is, see above subheading
// your other code
});
How the Code Works
A brief explanation of the JavaScript: a class is added to each photo's container <div>
, depending on the number of photos in the row. So, instead of having CSS styling for every possible photoset layout, only those classes are styled.
instructionsinstall