Last active
December 14, 2015 13:19
-
-
Save azproduction/5092884 to your computer and use it in GitHub Desktop.
http://500px.com/popular bookmarklet
This file contains 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
<a href="javascript: void $.getScript('https://gist.github.com/azproduction/5092884/raw/2e2f414b04216770b05bb3d293e8efd741e91b3d/bookmarklet.js');">500px Tiles!</a> |
This file contains 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
(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(); | |
}); | |
}()); |
This file contains 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
$.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