From b295f68312aece35475c1373f78776538d5d0a26 Mon Sep 17 00:00:00 2001 From: Jonathan Catmull Date: Mon, 2 May 2016 17:08:57 +0100 Subject: [PATCH] Added on load handler which includes offset --- jquery.singlePageNav.js | 28 ++++++++++++++++++++++++++-- jquery.singlePageNav.min.js | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/jquery.singlePageNav.js b/jquery.singlePageNav.js index b7f8dfa..3241def 100644 --- a/jquery.singlePageNav.js +++ b/jquery.singlePageNav.js @@ -17,7 +17,7 @@ if (typeof Object.create !== 'function') { (function($, window, document, undefined) { "use strict"; - + var SinglePageNav = { init: function(options, container) { @@ -37,6 +37,8 @@ if (typeof Object.create !== 'function') { this.$links.on('click.singlePageNav', $.proxy(this.handleClick, this)); + $( document ).ready($.proxy(this.handleLoad, this)); + this.didScroll = false; this.checkPosition(); this.setTimer(); @@ -76,6 +78,28 @@ if (typeof Object.create !== 'function') { }); } }, + + handleLoad: function() { + var self = this, + hash = window.location.hash, + $elem = $(hash); + + // e.preventDefault(); + + if ($elem.length) { // Make sure the target elem exists + + // Prevent active link from cycling during the scroll + self.clearTimer(); + + self.setActiveLink(hash); + self.$htmlbody.scrollTop(self.getCoords($elem).top); + + setTimeout(function(){ + self.$htmlbody.scrollTop(self.getCoords($elem).top); + self.setTimer(); + },200); + } + }, scrollTo: function($elem, callback) { var self = this; @@ -181,4 +205,4 @@ if (typeof Object.create !== 'function') { beforeStart: false }; -})(jQuery, window, document); +})(jQuery, window, document); \ No newline at end of file diff --git a/jquery.singlePageNav.min.js b/jquery.singlePageNav.min.js index f0a4feb..908fb46 100644 --- a/jquery.singlePageNav.min.js +++ b/jquery.singlePageNav.min.js @@ -5,4 +5,4 @@ * @author Chris Wojcik * @version 1.2.0 */ -if(typeof Object.create!=="function"){Object.create=function(e){function t(){}t.prototype=e;return new t}}(function(e,t,n,r){"use strict";var i={init:function(n,r){this.options=e.extend({},e.fn.singlePageNav.defaults,n);this.container=r;this.$container=e(r);this.$links=this.$container.find("a");if(this.options.filter!==""){this.$links=this.$links.filter(this.options.filter)}this.$window=e(t);this.$htmlbody=e("html, body");this.$links.on("click.singlePageNav",e.proxy(this.handleClick,this));this.didScroll=false;this.checkPosition();this.setTimer()},handleClick:function(t){var n=this,r=t.currentTarget,i=e(r.hash);t.preventDefault();if(i.length){n.clearTimer();if(typeof n.options.beforeStart==="function"){n.options.beforeStart()}n.setActiveLink(r.hash);n.scrollTo(i,function(){if(n.options.updateHash&&history.pushState){history.pushState(null,null,r.hash)}n.setTimer();if(typeof n.options.onComplete==="function"){n.options.onComplete()}})}},scrollTo:function(e,t){var n=this;var r=n.getCoords(e).top;var i=false;n.$htmlbody.stop().animate({scrollTop:r},{duration:n.options.speed,easing:n.options.easing,complete:function(){if(typeof t==="function"&&!i){t()}i=true}})},setTimer:function(){var e=this;e.$window.on("scroll.singlePageNav",function(){e.didScroll=true});e.timer=setInterval(function(){if(e.didScroll){e.didScroll=false;e.checkPosition()}},250)},clearTimer:function(){clearInterval(this.timer);this.$window.off("scroll.singlePageNav");this.didScroll=false},checkPosition:function(){var e=this.$window.scrollTop();var t=this.getCurrentSection(e);this.setActiveLink(t)},getCoords:function(e){return{top:Math.round(e.offset().top)-this.options.offset}},setActiveLink:function(e){var t=this.$container.find("a[href$='"+e+"']");if(!t.hasClass(this.options.currentClass)){this.$links.removeClass(this.options.currentClass);t.addClass(this.options.currentClass)}},getCurrentSection:function(t){var n,r,i,s;for(n=0;n=i.top-this.options.threshold){s=r}}}return s||this.$links[0].hash}};e.fn.singlePageNav=function(e){return this.each(function(){var t=Object.create(i);t.init(e,this)})};e.fn.singlePageNav.defaults={offset:0,threshold:120,speed:400,currentClass:"current",easing:"swing",updateHash:false,filter:"",onComplete:false,beforeStart:false}})(jQuery,window,document) \ No newline at end of file +"function"!=typeof Object.create&&(Object.create=function(t){function i(){}return i.prototype=t,new i}),function(t,i,n,o){"use strict";var e={init:function(o,e){this.options=t.extend({},t.fn.singlePageNav.defaults,o),this.container=e,this.$container=t(e),this.$links=this.$container.find("a"),""!==this.options.filter&&(this.$links=this.$links.filter(this.options.filter)),this.$window=t(i),this.$htmlbody=t("html, body"),this.$links.on("click.singlePageNav",t.proxy(this.handleClick,this)),t(n).ready(t.proxy(this.handleLoad,this)),this.didScroll=!1,this.checkPosition(),this.setTimer()},handleClick:function(i){var n=this,o=i.currentTarget,e=t(o.hash);i.preventDefault(),e.length&&(n.clearTimer(),"function"==typeof n.options.beforeStart&&n.options.beforeStart(),n.setActiveLink(o.hash),n.scrollTo(e,function(){n.options.updateHash&&history.pushState&&history.pushState(null,null,o.hash),n.setTimer(),"function"==typeof n.options.onComplete&&n.options.onComplete()}))},handleLoad:function(){var n=this,o=i.location.hash,e=t(o);e.length&&(n.clearTimer(),n.setActiveLink(o),n.$htmlbody.scrollTop(n.getCoords(e).top),setTimeout(function(){n.$htmlbody.scrollTop(n.getCoords(e).top),n.setTimer()},200))},scrollTo:function(t,i){var n=this,o=n.getCoords(t).top,e=!1;n.$htmlbody.stop().animate({scrollTop:o},{duration:n.options.speed,easing:n.options.easing,complete:function(){"function"!=typeof i||e||i(),e=!0}})},setTimer:function(){var t=this;t.$window.on("scroll.singlePageNav",function(){t.didScroll=!0}),t.timer=setInterval(function(){t.didScroll&&(t.didScroll=!1,t.checkPosition())},250)},clearTimer:function(){clearInterval(this.timer),this.$window.off("scroll.singlePageNav"),this.didScroll=!1},checkPosition:function(){var t=this.$window.scrollTop(),i=this.getCurrentSection(t);null!==i&&this.setActiveLink(i)},getCoords:function(t){return{top:Math.round(t.offset().top)-this.options.offset}},setActiveLink:function(t){var i=this.$container.find("a[href$='"+t+"']");i.hasClass(this.options.currentClass)||(this.$links.removeClass(this.options.currentClass),i.addClass(this.options.currentClass))},getCurrentSection:function(i){var n,o,e,s;for(n=0;n=e.top-this.options.threshold&&(s=o));return s||(0===this.$links.length?null:this.$links[0].hash)}};t.fn.singlePageNav=function(t){return this.each(function(){var i=Object.create(e);i.init(t,this)})},t.fn.singlePageNav.defaults={offset:0,threshold:120,speed:400,currentClass:"current",easing:"swing",updateHash:!1,filter:"",onComplete:!1,beforeStart:!1}}(jQuery,window,document); \ No newline at end of file