Skip to content

Instantly share code, notes, and snippets.

@kkaefer
Created January 9, 2011 00:42
Show Gist options
  • Save kkaefer/771281 to your computer and use it in GitHub Desktop.
Save kkaefer/771281 to your computer and use it in GitHub Desktop.
$(function() {
function pushState() {
var url = $(this).attr('href');
replaceImage(url);
if (history.pushState) {
history.pushState({}, '', url);
}
return false;
}
function attachHandlers() {
$('a.previous-link, a.next-link')
.unbind('click', pushState)
.bind('click', pushState);
}
function replaceImage(url) {
$('.body').load(url + ' .body', attachHandlers);
$('.photo').css('opacity', 0.5); // Loading indicator ;)
}
$(window).bind('popstate', function(e) {
if (e.originalEvent.state) {
replaceImage(location.href);
}
});
$(document).keydown(function(e){
if (e.shiftKey || e.metaKey || e.ctrlKey || e.altKey || e.srcElement !== document.body) return;
if (e.keyCode == 37) $('a.previous-link').eq(0).click();
else if (e.keyCode == 39) $('a.next-link').eq(0).click();
});
attachHandlers();
});
@kkaefer
Copy link
Author

kkaefer commented Jan 9, 2011

Replace the .body selector with the fragment of the page that you want to replace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment