1(function($, UI) { 2 3 "use strict"; 4 5 6 var $win = $(window), 7 8 ScrollSpy = function(element, options) { 9 10 var $element = $(element); 11 12 if($element.data("scrollspy")) return; 13 14 this.options = $.extend({}, ScrollSpy.defaults, options); 15 this.element = $(element); 16 17 var $this = this, idle, inviewstate, initinview, 18 fn = function(){ 19 20 var inview = isInView($this.element, $this.options); 21 22 if(inview && !inviewstate) { 23 24 if(idle) clearTimeout(idle); 25 26 if(!initinview) { 27 $this.element.addClass($this.options.initcls); 28 $this.offset = $this.element.offset(); 29 initinview = true; 30 31 $this.element.trigger("uk-scrollspy-init"); 32 } 33 34 idle = setTimeout(function(){ 35 36 if(inview) { 37 $this.element.addClass("uk-scrollspy-inview").addClass($this.options.cls).width(); 38 } 39 40 }, $this.options.delay); 41 42 inviewstate = true; 43 $this.element.trigger("uk.scrollspy.inview"); 44 } 45 46 if (!inview && inviewstate && $this.options.repeat) { 47 $this.element.removeClass("uk-scrollspy-inview").removeClass($this.options.cls); 48 inviewstate = false; 49 50 $this.element.trigger("uk.scrollspy.outview"); 51 } 52 }; 53 54 $win.on("scroll", fn).on("resize orientationchange", UI.Utils.debounce(fn, 50)); 55 56 fn(); 57 58 this.element.data("scrollspy", this); 59 }; 60 61 ScrollSpy.defaults = { 62 "cls" : "uk-scrollspy-inview", 63 "initcls" : "uk-scrollspy-init-inview", 64 "topoffset" : 0, 65 "leftoffset" : 0, 66 "repeat" : false, 67 "delay" : 0 68 }; 69 70 UI["scrollspy"] = ScrollSpy; 71 72 var ScrollSpyNav = function(element, options) { 73 74 var $element = $(element); 75 76 if($element.data("scrollspynav")) return; 77 78 this.element = $element; 79 this.options = $.extend({}, ScrollSpyNav.defaults, options); 80 81 var ids = [], 82 links = this.element.find("a[href^='#']").each(function(){ ids.push($(this).attr("href")); }), 83 targets = $(ids.join(",")); 84 85 var $this = this, inviews, fn = function(){ 86 87 inviews = []; 88 89 for(var i=0 ; i < targets.length ; i++) { 90 if(isInView(targets.eq(i), $this.options)) { 91 inviews.push(targets.eq(i)); 92 } 93 } 94 95 if(inviews.length) { 96 97 var scrollTop = $win.scrollTop(), 98 target = (function(){ 99 for(var i=0; i< inviews.length;i++){ 100 if(inviews[i].offset().top >= scrollTop){ 101 return inviews[i]; 102 } 103 } 104 })(); 105 106 if(!target) return; 107 108 if($this.options.closest) { 109 links.closest($this.options.closest).removeClass($this.options.cls).end().filter("a[href='#"+target.attr("id")+"']").closest($this.options.closest).addClass($this.options.cls); 110 } else { 111 links.removeClass($this.options.cls).filter("a[href='#"+target.attr("id")+"']").addClass($this.options.cls); 112 } 113 } 114 }; 115 116 if(this.options.smoothscroll && UI["smoothScroll"]) { 117 links.each(function(){ 118 new UI["smoothScroll"](this, $this.options.smoothscroll); 119 }); 120 } 121 122 fn(); 123 124 $win.on("scroll", fn).on("resize orientationchange", UI.Utils.debounce(fn, 50)); 125 126 this.element.data("scrollspynav", this); 127 }; 128 129 ScrollSpyNav.defaults = { 130 "cls" : 'uk-active', 131 "closest" : false, 132 "topoffset" : 0, 133 "leftoffset" : 0, 134 "smoothscroll" : false 135 }; 136 137 UI["scrollspynav"] = ScrollSpyNav; 138 139 // helper 140 141 function isInView(element, options) { 142 143 var $element = element; 144 145 if (!$element.is(':visible')) { 146 return false; 147 } 148 149 var window_left = $win.scrollLeft(), window_top = $win.scrollTop(), offset = $element.offset(), left = offset.left, top = offset.top; 150 151 if (top + $element.height() >= window_top && top - options.topoffset <= window_top + $win.height() && 152 left + $element.width() >= window_left && left - options.leftoffset <= window_left + $win.width()) { 153 return true; 154 } else { 155 return false; 156 } 157 } 158 159 ScrollSpy.isInView = isInView; 160 161 // init code 162 $(document).on("uk-domready", function(e) { 163 $("[data-uk-scrollspy]").each(function() { 164 165 var element = $(this); 166 167 if (!element.data("scrollspy")) { 168 var obj = new ScrollSpy(element, UI.Utils.options(element.attr("data-uk-scrollspy"))); 169 } 170 }); 171 172 $("[data-uk-scrollspy-nav]").each(function() { 173 174 var element = $(this); 175 176 if (!element.data("scrollspynav")) { 177 var obj = new ScrollSpyNav(element, UI.Utils.options(element.attr("data-uk-scrollspy-nav"))); 178 } 179 }); 180 });
181 182})(jQuery, jQuery.UIkit);
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.