Skip to content

Instantly share code, notes, and snippets.

@rymai
Created January 18, 2013 09:59
Show Gist options
  • Save rymai/4563550 to your computer and use it in GitHub Desktop.
Save rymai/4563550 to your computer and use it in GitHub Desktop.
This is the script I use on http://everyday.rymai.me that allows you to move from one image to another while in lightbox mode. It requires jQuery (http://jquery.com) and Mousetrap (http://craig.is/killing/mice). Demo: http://everyday.rymai.me Notes: The *.js.coffee files should be pre-compiled by Sprockets (https://github.com/sstephenson/sprockets
// This code is the automatic conversion to JS of photos_carousel.js.coffee + application.js.coffee.
// Performed on http://coffeescript.org (click on the "TRY COFFEESCRIPT" tab).
var PhotosCarousel;
PhotosCarousel = (function() {
function PhotosCarousel(lightboxes) {
this.lightboxes = lightboxes;
this.currentIndex = 0;
this.setupObservers();
}
PhotosCarousel.prototype.setupObservers = function() {
var _this = this;
this.lightboxes.each(function(index, lightbox) {
return $(lightbox).on('click', function(l) {
return _this.currentIndex = index;
});
});
Mousetrap.bind('right', function() {
return _this.right();
});
Mousetrap.bind('left', function() {
return _this.left();
});
Mousetrap.bind('down', function() {
return _this.down();
});
return Mousetrap.bind('up', function() {
return _this.up();
});
};
PhotosCarousel.prototype.right = function() {
return this.moveTo(1);
};
PhotosCarousel.prototype.left = function() {
return this.moveTo(-1);
};
PhotosCarousel.prototype.down = function() {
return this.moveTo(7);
};
PhotosCarousel.prototype.up = function() {
return this.moveTo(-7);
};
PhotosCarousel.prototype.moveTo = function(step) {
this.currentIndex = (this.currentIndex + step) % this.lightboxes.size();
if (this.currentIndex < 0) {
this.currentIndex = this.lightboxes.size() + this.currentIndex;
}
return sublime.lightbox(this.lightboxes[this.currentIndex]).open();
};
return PhotosCarousel;
})();
$(document).ready(function() {
return window.photosCarousel = new PhotosCarousel($('a.sublime'));
});
#= require jquery
#= require mousetrap.min.js
#= require_self
#= require_tree .
$(document).ready ->
window.photosCarousel = new PhotosCarousel($('a.sublime'))
<!DOCTYPE html>
<html>
<head>
<script src="//cdn.sublimevideo.net/js/YOUR_TOKEN-beta.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="/mousetrap.min.js" type="text/javascript"></script>
<script src="/application.js" type="text/javascript"></script>
</head>
<body>
<a class="sublime" href="#photo_1">
<img class="everyday" src="thumbnail_1.jpg">
</a>
<img id="photo_1" src="photo_1.jpg" style="display:none">
<a class="sublime" href="#photo_2">
<img class="everyday" src="thumbnail_2.jpg">
</a>
<img id="photo_2" src="photo_2.jpg" style="display:none">
<a class="sublime" href="#photo_3">
<img class="everyday" src="thumbnail_3.jpg">
</a>
<img id="photo_3" src="photo_3.jpg" style="display:none">
<a class="sublime" href="#photo_4">
<img class="everyday" src="thumbnail_4.jpg">
</a>
<img id="photo_4" src="photo_4.jpg" style="display:none">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment