diff --git a/bower.json b/bower.json index 0bf1a1e..7831bb1 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "angular-smart-nav", "description": "Show or hide a navbar when scrolling in any particular direction and or minimise nav when scrolling pass the navbar", - "version": "0.1.0", + "version": "0.1.1", "homepage": "https://github.com/thisissoon/angular-smart-nav", "license": "MIT", "ignore": [ diff --git a/dist/angular-smart-nav.js b/dist/angular-smart-nav.js index 1e23774..ed0eba2 100644 --- a/dist/angular-smart-nav.js +++ b/dist/angular-smart-nav.js @@ -1,4 +1,4 @@ -/*! angular-smart-nav - v0.1.0 - 2016-06-10 */ +/*! angular-smart-nav - v0.1.1 - 2016-06-13 */ 'use strict'; /** * Module that detects the last scroll direction and @@ -38,7 +38,6 @@ angular.module('sn.smartNav', [ function ($window, $document){ return { restrict: 'A', - scope: {}, link: function($scope, $element){ /** * The last recorded scrollTop position @@ -54,6 +53,23 @@ angular.module('sn.smartNav', [ * @type {Boolean} */ var scrollingDown = false; + /** + * @method getScrollTop + * @returns {Number} the number of pixel from top of + * page the user has scrolled + */ + var getScrollTop = function getScrollTop() { + var doc = $document[0].documentElement, + body = $document[0].body, + scrollTop = ( (doc && doc.scrollTop) || (body && body.scrollTop) || 0 ); + + return scrollTop; + }; + /** + * The number of pixels from the top of the page + * @property {Number} positionFromTop + */ + var positionFromTop = (getScrollTop() + $element[0].getBoundingClientRect().top); /** * @method isScrollingDown * @private @@ -108,9 +124,8 @@ angular.module('sn.smartNav', [ * @private * @method calAffixedMode */ - var calAffixedMode = function calAffixedMode(){ - var positionFromTop = $element[0].getBoundingClientRect().top; - if (positionFromTop <= 0 ) { + var calAffixedMode = function calAffixedMode(scrollTop){ + if (scrollTop >= positionFromTop ) { $element.addClass('affix'); } else { $element.removeClass('affix'); @@ -124,13 +139,11 @@ angular.module('sn.smartNav', [ * @method onScroll */ var onScroll = function onScroll() { - var doc = $document[0].documentElement, - body = $document[0].body, - scrollTop = ( (doc && doc.scrollTop) || (body && body.scrollTop) || 0 ); + var scrollTop = getScrollTop(); calScrollDir(scrollTop); calMinimisedMode(scrollTop); - calAffixedMode(); + calAffixedMode(scrollTop); lastScrollTop = scrollTop; }; @@ -144,6 +157,8 @@ angular.module('sn.smartNav', [ $scope.$on('$destroy', onDestroy); angular.element($window).on('scroll', onScroll); + + onScroll(); } }; } diff --git a/dist/angular-smart-nav.min.js b/dist/angular-smart-nav.min.js index 850e530..7dbf58f 100644 --- a/dist/angular-smart-nav.min.js +++ b/dist/angular-smart-nav.min.js @@ -1,4 +1,4 @@ -/*! angular-smart-nav - v0.1.0 - 2016-06-10 */ +/*! angular-smart-nav - v0.1.1 - 2016-06-13 */ -!function(a){"use strict";angular.module("sn.smartNav",[]).directive("snSmartNav",["$window","$document",function(a,b){return{restrict:"A",scope:{},link:function(c,d){var e=0,f=!1,g=function(a){return a>e},h=function(a){return e>a},i=function(a){f&&h(a)?(f=!1,d.removeClass("scrolling-down"),d.addClass("scrolling-up")):!f&&g(a)&&(f=!0,d.removeClass("scrolling-up"),d.addClass("scrolling-down"))},j=function(a){a>d[0].offsetHeight?d.addClass("minimised-mode"):d.removeClass("minimised-mode")},k=function(){var a=d[0].getBoundingClientRect().top;0>=a?d.addClass("affix"):d.removeClass("affix")},l=function(){var a=b[0].documentElement,c=b[0].body,d=a&&a.scrollTop||c&&c.scrollTop||0;i(d),j(d),k(),e=d},m=function(){angular.element(a).off("scroll",l)};c.$on("$destroy",m),angular.element(a).on("scroll",l)}}}])}(window); +!function(a){"use strict";angular.module("sn.smartNav",[]).directive("snSmartNav",["$window","$document",function(a,b){return{restrict:"A",link:function(c,d){var e=0,f=!1,g=function(){var a=b[0].documentElement,c=b[0].body,d=a&&a.scrollTop||c&&c.scrollTop||0;return d},h=g()+d[0].getBoundingClientRect().top,i=function(a){return a>e},j=function(a){return e>a},k=function(a){f&&j(a)?(f=!1,d.removeClass("scrolling-down"),d.addClass("scrolling-up")):!f&&i(a)&&(f=!0,d.removeClass("scrolling-up"),d.addClass("scrolling-down"))},l=function(a){a>d[0].offsetHeight?d.addClass("minimised-mode"):d.removeClass("minimised-mode")},m=function(a){a>=h?d.addClass("affix"):d.removeClass("affix")},n=function(){var a=g();k(a),l(a),m(a),e=a},o=function(){angular.element(a).off("scroll",n)};c.$on("$destroy",o),angular.element(a).on("scroll",n),n()}}}])}(window); //# sourceMappingURL=angular-smart-nav.min.js.map \ No newline at end of file diff --git a/package.json b/package.json index 3de167c..e3d3353 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "angular-smart-nav", "private": false, - "version": "0.1.0", + "version": "0.1.1", "description": "Show or hide a navbar when scrolling in any particular direction and or minimise nav when scrolling pass the navbar", "repository": "https://github.com/thisissoon/angular-smart-nav", "license": "MIT",