4 lines
6.8 KiB
JavaScript
Executable File
4 lines
6.8 KiB
JavaScript
Executable File
/*! scrollNav - v2.2.0 - 2014-03-14
|
|
* http://scrollnav.com
|
|
* Copyright (c) 2014 James Wilson; Licensed MIT */
|
|
(function(s){var t=function(t,e,i,n){if(s(t).length>0){var a=s(t).offset().top;e=n?e:0,s("html:not(:animated),body:not(:animated)").animate({scrollTop:a-i},e)}},e=function(){return window.location.hash},i={classes:{loading:"sn-loading",failed:"sn-failed",success:"sn-active"},defaults:{sections:"h2",subSections:!1,sectionElem:"section",className:"scroll-nav",showHeadline:!0,headlineText:"Scroll To",showTopLink:!0,topLinkText:"Top",fixedMargin:40,scrollOffset:40,animated:!0,speed:500,insertLocation:"insertBefore",arrowKeys:!1,onInit:null,onRender:null,onDestroy:null},_set_body_class:function(t){var e=s("body");"loading"===t?e.addClass(i.classes.loading):"success"===t?e.removeClass(i.classes.loading).addClass(i.classes.success):e.removeClass(i.classes.loading).addClass(i.classes.failed)},_find_sections:function(t){var e=i.settings.sections,n=[];if(i.settings.showTopLink){var a=t.children().first();a.is(e)||n.push(a.nextUntil(e).andSelf())}t.find(e).each(function(){n.push(s(this).nextUntil(e).andSelf())}),i.sections={raw:n}},_setup_sections:function(t){var e=[];s(t).each(function(t){var n=[],a=s(this),o="scrollNav-"+(t+1),l=function(){return 0===t},c=function(){return!a.eq(0).is(i.settings.sections)},r=i.settings.showTopLink&&l()&&c()?i.settings.topLinkText:a.filter(i.settings.sections).text();if(a.wrapAll("<"+i.settings.sectionElem+' id="'+o+'" class="'+i.settings.className+'__section" />'),i.settings.subSections){var d=a.filter(i.settings.subSections);d.length>0&&d.each(function(t){var e=o+"-"+(t+1),l=s(this).text(),c=a.filter(s(this).nextUntil(d).andSelf());c.wrapAll('<div id="'+e+'" class="'+i.settings.className+'__sub-section" />'),n.push({id:e,text:l})})}e.push({id:o,text:r,sub_sections:n})}),i.sections.data=e},_tear_down_sections:function(t){s(t).each(function(){var t=this.sub_sections;s("#"+this.id).children().unwrap(),t.length>0&&s(t).each(function(){s("#"+this.id).children().unwrap()})})},_setup_nav:function(t){var e=s("<span />",{"class":i.settings.className+"__heading",text:i.settings.headlineText}),n=s("<div />",{"class":i.settings.className+"__wrapper"}),a=s("<nav />",{"class":i.settings.className,role:"navigation"}),o=s("<ol />",{"class":i.settings.className+"__list"});s.each(t,function(t){var e,n=0===t?s("<li />",{"class":i.settings.className+"__item active"}):s("<li />",{"class":i.settings.className+"__item"}),a=s("<a />",{href:"#"+this.id,"class":i.settings.className+"__link",text:this.text});this.sub_sections.length>0&&(n.addClass("is-parent-item"),e=s("<ol />",{"class":i.settings.className+"__sub-list"}),s.each(this.sub_sections,function(){var t=s("<li />",{"class":i.settings.className+"__sub-item"}),n=s("<a />",{href:"#"+this.id,"class":i.settings.className+"__sub-link",text:this.text});e.append(t.append(n))})),o.append(n.append(a).append(e))}),i.settings.showHeadline?a.append(n.append(e).append(o)):a.append(n.append(o)),i.nav=a},_insert_nav:function(){var s=i.settings.insertLocation,t=i.settings.insertTarget;i.nav[s](t)},_setup_pos:function(){var t=i.nav,e=s(window).height(),n=t.offset().top;s.each(i.sections.data,function(){var t=s("#"+this.id),e=t.height();this.top_offset=t.offset().top,this.bottom_offset=this.top_offset+e}),i.dims={vp_height:e,nav_offset:n}},_check_pos:function(){var t=i.nav,e=s(window).scrollTop(),n=e+i.settings.scrollOffset,a=e+i.dims.vp_height-i.settings.scrollOffset,o=[];e>i.dims.nav_offset-i.settings.fixedMargin?t.addClass("fixed"):t.removeClass("fixed"),s.each(i.sections.data,function(){(this.top_offset>n&&a>this.top_offset||this.bottom_offset>n&&a>this.bottom_offset||n>this.top_offset&&this.bottom_offset>a)&&o.push(this)}),t.find("."+i.settings.className+"__item").removeClass("active").removeClass("in-view"),s.each(o,function(s){0===s?t.find('a[href="#'+this.id+'"]').parents("."+i.settings.className+"__item").addClass("active").addClass("in-view"):t.find('a[href="#'+this.id+'"]').parents("."+i.settings.className+"__item").addClass("in-view"),s++,i.sections.active=o})},_init_scroll_listener:function(){s(window).on("scroll.scrollNav",function(){i._check_pos()})},_rm_scroll_listeners:function(){s(window).off("scroll.scrollNav")},_init_resize_listener:function(){s(window).on("resize.scrollNav",function(){i._setup_pos(),i._check_pos()})},_rm_resize_listener:function(){s(window).off("resize.scrollNav")},_init_click_listener:function(){s("."+i.settings.className).find("a").on("click.scrollNav",function(e){e.preventDefault();var n=s(this).attr("href"),a=i.settings.speed,o=i.settings.scrollOffset,l=i.settings.animated;t(n,a,o,l)})},_rm_click_listener:function(){s("."+i.settings.className).find("a").off("click.scrollNav")},_init_keyboard_listener:function(e){i.settings.arrowKeys&&s(document).on("keydown.scrollNav",function(s){if(40===s.keyCode||38===s.keyCode){var n=function(s){var t=0,n=e.length;for(t;n>t;t++)if(e[t].id===i.sections.active[0].id){var a=40===s?t+1:t-1,o=void 0===e[a]?void 0:e[a].id;return o}},a=n(s.keyCode);if(void 0!==a){s.preventDefault();var o="#"+a,l=i.settings.speed,c=i.settings.scrollOffset,r=i.settings.animated;t(o,l,c,r)}}})},_rm_keyboard_listener:function(){s(document).off("keydown.scrollNav")},init:function(n){return this.each(function(){var a=s(this);i.settings=s.extend({},i.defaults,n),i.settings.insertTarget=i.settings.insertTarget?s(i.settings.insertTarget):a,a.length>0?(i.settings.onInit&&i.settings.onInit.call(this),i._set_body_class("loading"),i._find_sections(a),a.find(i.settings.sections).length>0?(i._setup_sections(i.sections.raw),i._setup_nav(i.sections.data),i.settings.insertTarget.length>0?(i._insert_nav(),i._setup_pos(),i._check_pos(),i._init_scroll_listener(),i._init_resize_listener(),i._init_click_listener(),i._init_keyboard_listener(i.sections.data),i._set_body_class("success"),t(e()),i.settings.onRender&&i.settings.onRender.call(this)):(console.log('Build failed, scrollNav could not find "'+i.settings.insertTarget+'"'),i._set_body_class("failed"))):(console.log('Build failed, scrollNav could not find any "'+i.settings.sections+'s" inside of "'+a.selector+'"'),i._set_body_class("failed"))):(console.log('Build failed, scrollNav could not find "'+a.selector+'"'),i._set_body_class("failed"))})},destroy:function(){return this.each(function(){i._rm_scroll_listeners(),i._rm_resize_listener(),i._rm_click_listener(),i._rm_keyboard_listener(),s("body").removeClass("sn-loading sn-active sn-failed"),s("."+i.settings.className).remove(),i._tear_down_sections(i.sections.data),i.settings.onDestroy&&i.settings.onDestroy.call(this),i.settings=[],i.sections=void 0})}};s.fn.scrollNav=function(){var t,e=arguments[0];if(i[e])e=i[e],t=Array.prototype.slice.call(arguments,1);else{if("object"!=typeof e&&e)return s.error("Method "+e+" does not exist in the scrollNav plugin"),this;e=i.init,t=arguments}return e.apply(this,t)}})(jQuery); |