solo-third-skins/skins/dashu03/js/bootstrap-scrollspy.min.js
2019-03-16 19:01:27 +08:00

1 line
1.8 KiB
JavaScript

!function(r){"use strict";function i(t,s){var e,o=r.proxy(this.process,this),i=r(t).is("body")?r(window):r(t);this.options=r.extend({},r.fn.scrollspy.defaults,s),this.$scrollElement=i.on("scroll.scroll-spy.data-api",o),this.selector=(this.options.target||(e=r(t).attr("href"))&&e.replace(/.*(?=#[^\s]+$)/,"")||"")+" .nav li > a",this.$body=r("body"),this.refresh(),this.process()}i.prototype={constructor:i,refresh:function(){var t=this;this.offsets=r([]),this.targets=r([]),this.$body.find(this.selector).map(function(){var t=r(this),s=t.data("target")||t.attr("href"),e=/^#\w/.test(s)&&r(s);return e&&e.length&&[[e.position().top,s]]||null}).sort(function(t,s){return t[0]-s[0]}).each(function(){t.offsets.push(this[0]),t.targets.push(this[1])})},process:function(){var t,s=this.$scrollElement.scrollTop()+this.options.offset,e=(this.$scrollElement[0].scrollHeight||this.$body[0].scrollHeight)-this.$scrollElement.height(),o=this.offsets,i=this.targets,r=this.activeTarget;if(e<=s)return r!=(t=i.last()[0])&&this.activate(t);for(t=o.length;t--;)r!=i[t]&&s>=o[t]&&(!o[t+1]||s<=o[t+1])&&this.activate(i[t])},activate:function(t){var s,e;this.activeTarget=t,r(this.selector).parent(".active").removeClass("active"),e=this.selector+'[data-target="'+t+'"],'+this.selector+'[href="'+t+'"]',(s=r(e).parent("li").addClass("active")).parent(".dropdown-menu").length&&(s=s.closest("li.dropdown").addClass("active")),s.trigger("activate")}},r.fn.scrollspy=function(o){return this.each(function(){var t=r(this),s=t.data("scrollspy"),e="object"==typeof o&&o;s||t.data("scrollspy",s=new i(this,e)),"string"==typeof o&&s[o]()})},r.fn.scrollspy.Constructor=i,r.fn.scrollspy.defaults={offset:10},r(window).on("load",function(){r('[data-spy="scroll"]').each(function(){var t=r(this);t.scrollspy(t.data())})})}(window.jQuery);