jquery.gallery.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. (function($) {
  2. 'use strict';
  3. $.fn.gallery = function(options) {
  4. var defaults = {
  5. current: 0,
  6. classes: ''
  7. };
  8. options = $.extend(defaults, options);
  9. return this.each(function() {
  10. var $gallery = $(this),
  11. $galleryItems = $gallery.children();
  12. $gallery
  13. .addClass('gallery')
  14. .addClass(options.classes)
  15. .height($galleryItems.height())
  16. .attr('tabindex', 1);
  17. $galleryItems
  18. .addClass('gallery-item')
  19. .eq(options.current)
  20. .addClass('current');
  21. $gallery.on('keyup', function(event) {
  22. var $current = $('.current', $gallery);
  23. var index = $current.index(),
  24. newIndex;
  25. $current.removeClass('current');
  26. if (index === $galleryItems.length - 1 || index === 0) {
  27. $gallery.trigger('end-of-world');
  28. }
  29. switch (event.which) {
  30. case 39:
  31. newIndex = index === $galleryItems.length - 1 ? 0 : index + 1;
  32. break;
  33. case 37:
  34. newIndex = index === 0 ? $galleryItems.length - 1 : index - 1;
  35. break;
  36. }
  37. $galleryItems.eq(newIndex).addClass('current');
  38. });
  39. });
  40. }
  41. })(jQuery);