-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.headindex.min.js
5 lines (5 loc) · 5.25 KB
/
jquery.headindex.min.js
1
2
3
4
5
/*!
* jquery_headindex
* https://github.com/xxyangyoulin/jquery_headindex
*/
!function(t,e){var i=function(){function e(e,i){this.settings=t.extend({},t.fn.headIndex.def,i||{}),this.element=e,this.init()}return e.prototype={init:function(){this.articleWrap=t(this.settings.articleWrapSelector),this.headerList=this.settings.excludeSelector?this.articleWrap.find(":header").not(this.settings.excludeSelector):this.articleWrap.find(":header"),this.indexBox=t(this.settings.indexBoxSelector),this.scrollBody=t(this.settings.scrollSelector),this.scrollWrap=t(this.settings.scrollWrap),this.manual=!1;var e=t(this.settings.indexBoxWrap);if(0===this.indexBox.length||0===this.headerList.length)return e.length>0&&e.hide(),this.settings.onHide(),null;e.length>0&&e.is(":hidden")&&e.fadeIn(),this.initHeader(),this.event()},initHeader:function(){for(var e=0;e<this.headerList.length;e++,this.autoId++)this.headerList[e].id=this.headerList[e].id||"header-id-"+this.autoId,this.headerList[e].topHeight=this.offsetTop(this.headerList[e]),this.headerList[e].h=Number(this.headerList[e].tagName.charAt(1));this.tempHtml=[],this.buildHtml(this.buildTree());var i="<ul>"+this.tempHtml.join("")+"</ul>";this.indexBox.html(i),this.settings.hasDynamicEffect||t("."+this.settings.subItemBoxClass).show()},event:function(){const e=this;var i=null;this.indexBox.on("click.headindex",function(s){var h=t(s.target);if(h.hasClass(e.settings.linkClass)){s.preventDefault();var n=h.parent("."+e.settings.itemClass);e.manual=!0,i&&(clearTimeout(i),i=null),i=setTimeout(function(){e.manual=!1},300),e.current(n),e.scrollTo(s.target.getAttribute("href"))}}),this.scrollEventFun=function(){e.manual||e.updateCurrent()},t(this.scrollWrap).scroll(this.scrollEventFun),e.updateCurrent()},updateTopHeight:function(){var t,e=this.headerList.length;if(0!==e&&(this.headerList[0].topHeight!==this.offsetTop(this.headerList[0])||this.headerList[e-1].topHeight!==this.offsetTop(this.headerList[e-1])))if(this.headerList[0].topHeight-this.offsetTop(this.headerList[0])!=this.headerList[e-1].topHeight-this.offsetTop(this.headerList[e-1]))for(t=0;t<this.headerList.length;t++,this.autoId++)this.headerList[t].topHeight=this.offsetTop(this.headerList[t]);else{var i=this.offsetTop(this.headerList[0])-this.headerList[0].topHeight;for(t=0;t<this.headerList.length;t++,this.autoId++)this.headerList[t].topHeight+=i}},current:function(t){var e;if(0!==t.length&&!t.hasClass("current")){var i=this.indexBox.find("li.current");if(i.length>0&&i.removeClass("current"),t.addClass("current"),this.settings.hasDynamicEffect){this.indexBox.find("ul.open").removeClass("open"),(e=t.children("."+this.settings.subItemBoxClass)).length>0&&e.addClass("open").slideDown();var s=t.parents("ul."+this.settings.subItemBoxClass);s.length>0&&s.addClass("open").slideDown(),(e=this.indexBox.find("ul."+this.settings.subItemBoxClass).not(".open")).length>0&&e.slideUp()}}},buildHtml:function(t){if(void 0!==t&&0!==t.length)for(var e=0;e<t.length;e++)this.tempHtml.push("<li class='"+this.settings.itemClass+"'><a class='"+this.settings.linkClass+"' href='#"+t[e].item.id+"'>"+t[e].item.innerText+"</a>"),0!==t[e].children.length&&(this.tempHtml.push("<ul class='"+this.settings.subItemBoxClass+"'>"),this.buildHtml(t[e].children),this.tempHtml.push("</ul>")),this.tempHtml.push("</li>")},buildTree:function(){for(var t,e=null,i=[],s=0;s<this.headerList.length;s++)if(null!=e)if(e.item.h<this.headerList[s].h)t={item:this.headerList[s],parent:e,children:[]},e.children.push(t),e=t;else if(e.item.h!==this.headerList[s].h){for(;null!=e&&e.item.h>this.headerList[s].h;)e=e.parent;null!=e?s--:(e={item:this.headerList[s],parent:null,children:[]},i.push(e))}else t={item:this.headerList[s],parent:e.parent,children:[]},(e.parent&&e.parent.children||i).push(t),e=t;else e={item:this.headerList[s],parent:null,children:[]},i.push(e);return i},search:function(t,e,i){if(0===this.headerList.length)return null;if(e-t<=1)return this.headerList[e].topHeight<i?this.headerList[e]:this.headerList[t];if(t<e){var s=parseInt((t+e)/2),h=this.headerList[s].topHeight;if(i<h)e=s;else{if(!(i>h))return this.headerList[s];t=s}return this.search(t,e,i)}},offsetTop:function(t){return t.offsetTop-this.settings.offset},scrollTo:function(t){this.scrollBody.stop().animate({scrollTop:this.offsetTop(document.querySelector(t))},"fast")},updateCurrent:function(){var t=this.scrollBody.scrollTop();this.updateTopHeight();var e=this.search(0,this.headerList.length-1,t);if(e){var i=this.indexBox.find('a[href="#'+e.id+'"]').parent("li."+this.settings.itemClass);this.current(i)}},clean:function(){this.indexBox.html(""),this.element&&(this.indexBox.unbind("click.headindex"),t(this.scrollWrap).unbind("scroll",this.scrollEventFun),this.element.data("headIndex",null))},ignoreScrollEvent:function(t){t?this.manual=!0:(this.manual=!1,this.updateCurrent())}},e.prototype.autoId=1,e}();t.fn.headIndex=function(e){return this.each(function(){var s=t(this),h=s.data("headIndex");if(h||(h=new i(s,e),s.data("headIndex",h)),"string"===t.type(e))return h[e]()})},t.fn.headIndex.def={articleWrapSelector:".article-wrap",indexBoxSelector:".index-box",indexBoxWrap:null,scrollSelector:"body,html",scrollWrap:e,hasDynamicEffect:!0,excludeSelector:null,offset:0,subItemBoxClass:"index-subItem-box",itemClass:"index-item",linkClass:"index-link",onHide:function(){}}}(jQuery,window);