1(function($) { 2 $.fn.smoothScroller = function(options) { 3 options = $.extend({}, $.fn.smoothScroller.defaults, options); 4 var el = $(this); 5 6 $(options.scrollEl).animate({ 7 scrollTop: el.offset().top - $(options.scrollEl).offset().top - options.offset 8 }, options.speed, options.ease, function() { 9 var hash = el.attr('id'); 10 11 if(hash.length) { 12 if(history.pushState) { 13 // history.pushState(null, null, '#' + hash); 14 } else { 15 document.location.hash = hash; 16 } 17 } 18 19 el.trigger('smoothScrollerComplete'); 20 }); 21 22 return this; 23 }; 24 25 $.fn.smoothScroller.defaults = { 26 speed: 400, 27 ease: 'swing', 28 scrollEl: 'body,html', 29 offset: 0 30 }; 31 32 //console.log(options.offset); 33 34 $('body').on('click', '[data-smoothscroller]', function(e) { 35 e.preventDefault(); 36 var href = $(this).attr('href'); 37 alert("sfdghj"); 38 if(href.indexOf('#') === 0) { 39 $(href).smoothScroller(); 40 } 41 }); 42}(jQuery)); 43 44(function($) { 45var verboseIdCache = {}; 46$.fn.toc = function(options) { 47 var self = this; 48 var opts = $.extend({}, jQuery.fn.toc.defaults, options); 49 50 var container = $(opts.container); 51 var headings = $(opts.selectors, container); 52 var headingOffsets = []; 53 var activeClassName = opts.activeClass; 54 55 var scrollTo = function(e, callback) { 56 if (opts.smoothScrolling && typeof opts.smoothScrolling === 'function') { 57 e.preventDefault(); 58 var elScrollTo = $(e.target).attr('href'); 59 60 opts.smoothScrolling(elScrollTo, opts, callback); 61 } 62 $('li', self).removeClass(activeClassName); 63 $(e.target).parent().addClass(activeClassName); 64 }; 65 66 //highlight on scroll 67 var timeout; 68 var highlightOnScroll = function(e) { 69 if (timeout) { 70 clearTimeout(timeout); 71 } 72 timeout = setTimeout(function() { 73 var top = $(window).scrollTop(), 74 highlighted, closest = Number.MAX_VALUE, index = 0; 75 76 for (var i = 0, c = headingOffsets.length; i < c; i++) { 77 var currentClosest = Math.abs(headingOffsets[i] - top); 78 if (currentClosest < closest) { 79 index = i; 80 closest = currentClosest; 81 } 82 } 83 84 $('li', self).removeClass(activeClassName); 85 highlighted = $('li:eq('+ index +')', self).addClass(activeClassName); 86 opts.onHighlight(highlighted); 87 }, 50); 88 }; 89 if (opts.highlightOnScroll) { 90 $(window).bind('scroll', highlightOnScroll); 91 highlightOnScroll(); 92 } 93 94 return this.each(function() { 95 //build TOC 96 var el = $(this); 97 var ul = $(opts.listType); 98 99 headings.each(function(i, heading) { 100 var $h = $(heading); 101 headingOffsets.push($h.offset().top - opts.highlightOffset); 102 103 var anchorName = opts.anchorName(i, heading, opts.prefix); 104 105 //add anchor 106 if(heading.id !== anchorName) { 107 var anchor = $('<span/>').attr('id', anchorName).insertBefore($h); 108 } 109 110 //build TOC item 111 var a = $('<a/>') 112 .text(opts.headerText(i, heading, $h)) 113 .attr('href', '#' + anchorName) 114 .bind('click', function(e) { 115 $(window).unbind('scroll', highlightOnScroll); 116 scrollTo(e, function() { 117 $(window).bind('scroll', highlightOnScroll); 118 }); 119 el.trigger('selected', $(this).attr('href')); 120 }); 121 122 var li = $('<li/>') 123 .addClass(opts.itemClass(i, heading, $h, opts.prefix)) 124 .append(a); 125 126 ul.append(li); 127 }); 128 el.html(ul); 129 }); 130}; 131 132 133jQuery.fn.toc.defaults = { 134 container: 'body', 135 listType: '<ul/>', 136 selectors: 'h1,h2,h3', 137 smoothScrolling: function(target, options, callback) { 138 $(target).smoothScroller({ 139 offset: options.scrollToOffset 140 }).on('smoothScrollerComplete', function() { 141 callback(); 142 }); 143 }, 144 scrollToOffset: 0, 145 prefix: 'toc', 146 activeClass: 'toc-active', 147 onHighlight: function() {}, 148 highlightOnScroll: true, 149 highlightOffset: 100, 150 anchorName: function(i, heading, prefix) { 151 if(heading.id.length) { 152 return heading.id; 153 } 154 155 var candidateId = $(heading).text().replace(/[^a-z0-9]/ig, ' ').replace(/\s+/g, '-').toLowerCase(); 156 if (verboseIdCache[candidateId]) { 157 var j = 2; 158 159 while(verboseIdCache[candidateId + j]) { 160 j++; 161 } 162 candidateId = candidateId + '-' + j; 163 164 } 165 verboseIdCache[candidateId] = true; 166 167 return prefix + '-' + candidateId; 168 }, 169 headerText: function(i, heading, $heading) { 170 return $heading.text(); 171 }, 172 itemClass: function(i, heading, $heading, prefix) { 173 return prefix + '-' + $heading[0].tagName.toLowerCase(); 174 } 175 176}; 177 178}
178)(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.