1/* 2 * Treehouse Scrollable 3 * 4 * Compatible with jQuery 1.4.4+ 5 * 6 * @author Marcin Kossakowski 7 * @author Joshua Lees 8 * 9 */ 10 11(function($) { 12 13 $.fn.thScrollable = function(options) { 14 15 var options = $.extend({ 16 showItems: 5, 17 prevElement: '.scrollable-prev', 18 nextElement: '.scrollable-next', 19 hideControls: false, 20 itemElement: 'li', 21 fadeSpeed: 500, 22 onFirst: function() {}, 23 onLast: function() {}, 24 onChange: function() {} 25 }, options); 26 27 28 29 return this.each(function() { 30 31 // Start 32 var $this = this; 33 var groups = []; 34 var currentIndex = 0; 35 var list = $(this).find(options.itemElement).hide().get(); 36 var evenGroupsCount = Math.floor(list.length / options.showItems); 37 38 var i = 0; 39 while (i < evenGroupsCount) { 40 groups[i] = options.showItems; 41 i++; 42 } 43 if (list.length - (groups.length * options.showItems) !== 0) { 44 groups.push(list.length - (groups.length * options.showItems)); 45 } 46 47 function groupIndex(mode) { 48 var max = groups.length; 49 var index = currentIndex; 50 51 if (mode == 'next') { 52 if (index < max) { 53 index++; 54 } 55 else { 56 index = 1; 57 } 58 } 59 else { 60 if (index > 1) { 61 index--; 62 } 63 else { 64 index = max; 65 } 66 } 67 68 return index; 69 } 70 71 function listShow(mode) { 72 var i = 0; 73 var pointer; 74 75 if (mode == 'prev') { 76 pointer = $(list).filter(":visible:first")[0] || $(list).first()[0]; 77 78 } 79 else if (mode == 'next') { 80 pointer = $(list).filter(":visible:last")[0] || $(list).last()[0]; 81 } 82 currentIndex = groupIndex(mode); 83 84 $(list).hide(); 85 86 while (i < groups[currentIndex - 1]) { 87 if (mode == 'prev') { 88 pointer = $(pointer).prev()[0] || $(list).last()[0]; 89 } 90 else if (mode == 'next') { 91 pointer = $(pointer).next()[0] || $(list).first()[0]; 92 } 93 94 95 $(pointer).fadeIn(options.fadeSpeed); 96 97 i++; 98 } 99 100 101 102 103 $($this).find(options.prevElement).unbind('click'); 104 $($this).find(options.prevElement).bind('click', function() { 105 listShow('prev'); 106 return false; 107 }); 108 109 $($this).find(options.nextElement).unbind('click'); 110 $($this).find(options.nextElement).bind('click', function() { 111 listShow('next'); 112 return false; 113 }); 114 115 if(groups.length > 1) { 116 $($this).find(options.nextElement).show(); 117 $($this).find(options.prevElement).show(); 118 } else { 119 $($this).find(options.nextElement).hide(); 120 $($this).find(options.prevElement).hide(); 121 } 122 123 if (currentIndex === 1) { 124 options.onFirst.call($this); 125 } else if (currentIndex === (groups.length)) { 126 options.onLast.call($this); 127 } 128 129 options.onChange.call($this, { 130 currentPage: currentIndex, 131 totalPages: groups.length 132 }); 133 134 }; 135 136 listShow('next'); 137 138 }); 139 }; 140})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.