-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathjquery.tocible.min.js
More file actions
11 lines (10 loc) · 2.41 KB
/
jquery.tocible.min.js
File metadata and controls
11 lines (10 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
/*
* jquery.tocible.min.js v1.2.0, jQuery Tocible
*
* Under MIT license, available at http://www.opensource.org/licenses/MIT.
*
* A lightweight table of contents navigation plugin
* https://github.com/markserbol/tocible
*
*/
(function(a){var k={heading:"h2",subheading:"h3",reference:".ref",title:"",hash:!1,offsetTop:50,speed:800,collapsible:!0,maxWidth:150};a.fn.tocible=function(m){var d=a.extend({},k,m);return this.each(function(){var b=a(this),e,f,g,h,l;b.find(".tocible").remove();f=b.find(d.reference);f.css({visibility:"hidden"});l=f.offset().left;e=a("<div/>",{"class":"tocible",html:"<ul/>"});e.css({"max-width":d.maxWidth});b.append(e).css({position:"relative"});if(d.title){var k=a(d.title).length?a(d.title).text():d.title;a("<div/>",{"class":"tocible_header",html:k+"<span/>"}).prependTo(e).click(function(){a(this).siblings("ul").slideToggle({duration:"slow",step:contain});a(this).find("span").toggleClass("toc_open")})}g=b.find(d.heading);h=b.find(d.subheading);g.add(h).each(function(){var c=a(this),e,b,f;e=c.attr("id")?"#"+c.attr("id"):"#";b=c.text();c.is(g)?f="heading":c.is(h)&&(f="subheading");b=a("<a/>",{text:b,href:e});a("<li/>",{"class":"tocible_"+f}).append(b).appendTo(".tocible > ul");b.click(function(b){b.preventDefault();b=c.offset();if(d.hash){var f=a(window).scrollTop();history.pushState?history.pushState({},document.title,e):(window.location.hash=e,a(window).scrollTop(f))}a("html, body").stop(!0).animate({scrollTop:b.top-10},d.speed)})});contain=function(){var c=a(window).scrollTop(),g=b.offset().top;e.css({top:d.offsetTop,bottom:"auto",left:l});g+b.outerHeight()<=c+e.height()+d.offsetTop?e.css({position:"absolute",bottom:0,top:"auto",left:f.position().left}):c>=g?e.css({position:"fixed",bottom:"auto",top:d.offsetTop,left:f.offset().left}):e.css({position:"absolute",left:f.position().left})};onScroll=function(){d.collapsible&&a(".tocible li.tocible_subheading").hide();g.add(h).each(function(c){var b=a(this).offset().top;c=a(".tocible li").eq(c);a(window).scrollTop()>=b-20?(c.addClass("toc_scrolled").siblings().removeClass("toc_scrolled"),d.collapsible&&(c.siblings().filter(".tocible_subheading").hide(),c.is(".tocible_subheading")?c.prevAll(".tocible_heading:first").nextUntil(".tocible_heading").show():c.is(".tocible_heading")&&c.nextUntil(".tocible_heading").show())):c.removeClass("toc_scrolled")})};a(window).on("resize scroll",function(a){contain();onScroll()}).trigger("scroll")})}})(jQuery);