Skip to content

Instantly share code, notes, and snippets.

@azproduction
Last active December 14, 2015 13:19
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 azproduction/5092884 to your computer and use it in GitHub Desktop.
Save azproduction/5092884 to your computer and use it in GitHub Desktop.
<a href="javascript: void $.getScript('https://gist.github.com/azproduction/5092884/raw/2e2f414b04216770b05bb3d293e8efd741e91b3d/bookmarklet.js');">500px Tiles!</a>
(function () {
var styleSheet = {
img: {
margin: '30px',
border: 'solid 1px #fff',
outline: 'solid 10px #000'
},
body: {
'text-align': 'center'
},
lightbox: {
'position': 'fixed',
'top': '50%',
'left': '50%',
'margin': null,
'outline': '9999px solid rgba(0,0,0,0.5)',
'max-height': '90%'
}
};
/**
* @param {Number} page
*
* @return {Promise} - resolve({jQuery} $tiles)
*/
function loadTiles(page) {
page = page || 1;
var dfd = $.Deferred(),
filter = '.photo_thumb .photo>a',
url = 'http://500px.com/popular?page=' + page;
// Load page
var $container = $('<div/>').load(url + ' ' + filter, function () {
dfd.resolve($container.find('a'));
});
return dfd.promise();
}
/**
* @param {jQuery} $tiles
*/
function appendTiles($tiles) {
$tiles.find('img').css(styleSheet.img);
$tiles.appendTo('body').hide().fadeIn();
}
/**
* @param {String} url
*
* @return {Promise} - resolve({jQuery} $img)
*/
function loadPhoto(url) {
var dfd = $.Deferred(),
filter = '#thephoto img';
// 1) load page and grab image
var $container = $('<div/>').load(url + ' ' + filter, function () {
var $img = $container.find('img');
// 2) then w8 for image load
$img.bind('load', function () {
dfd.resolve($img);
});
});
return dfd.promise();
}
/**
* @param {jQuery} $img
*/
function renderLightBox($img) {
$img.css(styleSheet.img)
.css(styleSheet.lightbox)
.addClass('js-lightbox')
.appendTo('body')
.hide()
.fadeIn();
$(window).bind('resize.lightbox', function () {
centrateLightBox($img);
})
.trigger('resize');
}
/**
* Wipes lightbox, clears events
*
* @param {jQuery|String} img
*/
function removeLightBox(img) {
var img = $(img);
$(window).unbind('.lightbox');
img.fadeOut(function () {
img.remove();
});
}
/**
* @param {jQuery} $img
*/
function centrateLightBox($img) {
$img.css({
'margin-left': $img.width() / -2,
'margin-top': $img.height() / -2
});
}
/**
* Wipes body; binds events
*/
function prepareBody() {
$('body').css(styleSheet.body).empty();
$(document)
// Open lightbox
.delegate('a', 'click', function (event) {
loadPhoto($(this).attr('href')).then(renderLightBox);
return false;
})
// Close lightbox
.click(function () {
removeLightBox('.js-lightbox');
});
}
/**
* Infinity scroll
*/
function infinityScroll() {
var $window = $(window),
$doc = $(document),
pageNumber = 1;
// Inifinity scroll
$doc.scroll(function () {
if ($window.scrollTop() >= ($doc.height() - $window.height())) {
$doc.trigger('scroll-bottom');
}
})
.bind('scroll-bottom', function () {
loadTiles(++pageNumber).then(appendTiles);
})
.trigger('scroll-bottom');
}
$(function () {
prepareBody();
infinityScroll();
});
}());
$.getScript('https://gist.github.com/azproduction/5092884/raw/2e2f414b04216770b05bb3d293e8efd741e91b3d/bookmarklet.js');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment