jquery.gallery.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <<<<<<< HEAD
  2. (function($) {
  3. 'use strict';
  4. $.fn.gallery = function(options) {
  5. var defaults = {
  6. current: 0,
  7. classes: "",
  8. arrow: false
  9. };
  10. return this.each(function() {
  11. var $gallery = $(this),
  12. $galleryItems = $gallery.children();
  13. options = $.extend(defaults, options);
  14. $gallery.addClass("gallery").addClass(options.classes);
  15. $galleryItems.addClass("gallery-item");
  16. $galleryItems.eq(options.current).addClass("current");
  17. $gallery.attr("tabindex", 0);
  18. var maxHeight = $($galleryItems[0]).height();
  19. for (var i = 1; i < $galleryItems.length; i++) {
  20. if ($($galleryItems[i]).height() > maxHeight) maxHeight = $($galleryItems[i]).height();
  21. }
  22. $gallery.height(maxHeight + "px");
  23. if (options.arrow) {
  24. $("<div>").addClass("arrow")
  25. .prepend( $("<span>").addClass("glyphicon glyphicon-chevron-left left") )
  26. .append( $("<span>").addClass("glyphicon glyphicon-chevron-right right") )
  27. .prependTo($galleryItems);
  28. }
  29. $gallery.find(".left").on("click", function() {
  30. var next;
  31. var $currentItem = $(".current", $gallery);
  32. next = $currentItem.index() - 1;
  33. var prev;
  34. if (next === $galleryItems.length - 1) {
  35. next = 0;
  36. }
  37. $galleryItems.removeClass("current").eq(next).addClass("current")
  38. });
  39. $gallery.find(".right").on("click", function() {
  40. var next;
  41. var $currentItem = $(".current", $gallery);
  42. next = $currentItem.index() + 1;
  43. if (next === $galleryItems.length) {
  44. next = 0;
  45. }
  46. $galleryItems.removeClass("current").eq(next).addClass("current")
  47. });
  48. $gallery.on("keyup", function(event) {
  49. var $currentItem = $(".current", $gallery);
  50. var next;
  51. if (event.which === 39) {
  52. next = $currentItem.index() + 1;
  53. if (next === $galleryItems.length) {
  54. next = 0;
  55. $gallery.trigger("gallery-event");
  56. }
  57. } else if (event.which === 37) {
  58. next = $currentItem.index() - 1;
  59. if (next < 0) next = $galleryItems.length - 1;
  60. }
  61. $galleryItems.removeClass("current").eq(next).addClass("current");
  62. });
  63. $(window).on('resize', function() {
  64. if (options.thumbs) $gallery.height( $('.current', $gallery).height() + $thumbs.innerHeight() );
  65. else $gallery.height( $('.current', $gallery).height() );
  66. });
  67. });
  68. };
  69. =======
  70. 'use strict';
  71. (function($){
  72. $.fn.gallery =function(options) {
  73. var defaults = {
  74. current: 0,
  75. classes: '',
  76. };
  77. var maxHeight,
  78. $arrowContain,
  79. $rightArrow,
  80. $leftArrow;
  81. function addArrow () {
  82. $arrowContain = $('<div>').css({
  83. 'position': 'absolute',
  84. 'width': '100%',
  85. 'top': maxHeight/2,
  86. 'font-size': '30px'
  87. })
  88. .appendTo('.gallery');
  89. $rightArrow = $('<a>').append('<span class="glyphicon glyphicon-chevron-right">')
  90. .addClass('pull-right')
  91. .appendTo($arrowContain);
  92. $leftArrow = $('<a>').append('<span class="glyphicon glyphicon-chevron-left">')
  93. .attr({'href': '#'})
  94. .css({'left': 300+'px'})
  95. .appendTo($arrowContain);
  96. }
  97. return this.each(function(){
  98. var $gallery = $(this),
  99. $galleryItems = $gallery.children();
  100. var $currentItem = $('.current', $gallery),
  101. next;
  102. options = $.extend(defaults, options)
  103. $gallery.addClass('gallery').addClass(options.classes);
  104. $galleryItems.addClass('gallery-item');
  105. $galleryItems.eq(options.current).addClass('current');
  106. $gallery.attr('tabindex', 0);
  107. maxHeight = $galleryItems.height();
  108. $gallery.height(maxHeight + 'px');
  109. addArrow();
  110. $rightArrow.on('click',function(){
  111. var $currentItem = $('.current', $gallery);
  112. next= $currentItem.index() + 1;
  113. if (next === $galleryItems.length) next = 0;
  114. $galleryItems.removeClass('current')
  115. .eq(next)
  116. .addClass('current');
  117. });
  118. $leftArrow.on('click',function(){
  119. var $currentItem = $('.current', $gallery);
  120. next= $currentItem.index() - 1;
  121. if (next < 0) next = $galleryItems.length - 1;
  122. $galleryItems.removeClass('current')
  123. .eq(next)
  124. .addClass('current');
  125. });
  126. $gallery.on('keyup', function(event){
  127. var $currentItem = $('.current', $gallery);
  128. // var next;
  129. if (event.which === 39) {
  130. next = $currentItem.index() + 1;
  131. if (next === $galleryItems.length) next = 0;
  132. $galleryItems.removeClass('current')
  133. .eq(next)
  134. .addClass('current');
  135. } else if (event.which === 37) {
  136. next = $currentItem.index() - 1;
  137. if (next < 0) next = $galleryItems.length - 1;
  138. $galleryItems.removeClass('current')
  139. .eq(next)
  140. .addClass('current');
  141. }
  142. });
  143. });
  144. };
  145. >>>>>>> 81998a7b2320275c76c2f6d969964b0e73055986
  146. })(jQuery);