From 905ef7c6bacd3ec6a09c69cd034a3d399c342079 Mon Sep 17 00:00:00 2001 From: Joel Arvidsson Date: Thu, 21 Nov 2013 23:32:49 +0100 Subject: [PATCH] Released 0.2.2 --- README.md | 2 +- angular-scroll.js | 31 ++++++++++++++++++++++--------- angular-scroll.min.js | 2 +- angular-scroll.min.js.map | 2 +- bower.json | 4 ++-- example/index.html | 3 ++- package.json | 2 +- 7 files changed, 30 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ba921bc..fc602b2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ angular-scroll ============== -Only dependent on AngularJS (no jQuery). 2.5K minified or 0.5K gzipped. +Only dependent on AngularJS (no jQuery). 2.6K minified or 0.6K gzipped. Example ------- diff --git a/angular-scroll.js b/angular-scroll.js index d1907b9..73c13fa 100644 --- a/angular-scroll.js +++ b/angular-scroll.js @@ -27,8 +27,16 @@ factory('scrollPosition', } }; + var getScrollY = function() { + return $window.scrollY || document.documentElement.scrollTop || document.body.scrollTop; + }; + + var getScrollX = function() { + return $window.scrollX || document.documentElement.scrollLeft || document.body.scrollLeft; + }; + angular.element($document).on('scroll', function(){ - lastScrollY = $window.scrollY || document.documentElement.scrollTop; + lastScrollY = getScrollY(); if(lastScrollY !== currentScrollY){ requestAnimation(executeCallbacks); @@ -38,13 +46,17 @@ factory('scrollPosition', return { observe : function(cb){ observers.push(cb); - } + }, + x: getScrollX, + y: getScrollY }; -}); + } +); + angular.module('duScroll.scroller', ['duScroll.requestAnimation']). factory('scroller', - function($window, requestAnimation) { + function($window, requestAnimation, scrollPosition) { function easeout(x) { return Math.pow(x, 0.7); @@ -56,8 +68,8 @@ factory('scroller', return; } var start = { - y: $window.scrollY, - x: $window.scrollX + y: scrollPosition.y(), + x: scrollPosition.x() }; var delta = { y: Math.round(y - start.y), @@ -82,7 +94,7 @@ factory('scroller', } function scrollDelta(x, y, duration){ - scrollTo($window.scrollX + (x || 0), $window.scrollY + (y || 0), duration); + scrollTo(scrollPosition.x() + (x || 0), scrollPosition.y() + (y || 0), duration); } return { @@ -92,6 +104,7 @@ factory('scroller', } ); + angular.module('duScroll.smoothScroll', ['duScroll.scroller']). directive('duSmoothScroll', function(scroller){ @@ -101,7 +114,7 @@ directive('duSmoothScroll', function(scroller){ element.on('click', function(e){ if(!$attr.href || $attr.href.indexOf('#') === -1) return; var elem = document.getElementById($attr.href.replace(/.*(?=#[^\s]+$)/, '').substring(1)); - if(!elem) return; + if(!elem || !elem.getBoundingClientRect) return; if (e.stopPropagation) e.stopPropagation(); if (e.preventDefault) e.preventDefault(); @@ -109,13 +122,13 @@ directive('duSmoothScroll', function(scroller){ var offset = -($attr.offset ? parseInt($attr.offset, 10) : 0); var pos = elem.getBoundingClientRect(); - var delta = pos.top; scroller.scrollDelta(0, pos.top + (isNaN(offset) ? 0 : offset), 1000); }); } }; }); + angular.module('duScroll.scrollspy', ['duScroll.scrollPosition']). directive('duScrollspy', function(scrollPosition) { var spies = []; diff --git a/angular-scroll.min.js b/angular-scroll.min.js index 9279ba4..9e35455 100644 --- a/angular-scroll.min.js +++ b/angular-scroll.min.js @@ -1,2 +1,2 @@ -angular.module("duScroll",["duScroll.scroller","duScroll.scrollPosition","duScroll.scrollspy","duScroll.requestAnimation","duScroll.smoothScroll"]),angular.module("duScroll.requestAnimation",[]).factory("requestAnimation",["$window","$timeout",function(a,b){return a.requestAnimationFrame||a.webkitRequestAnimationFrame||a.mozRequestAnimationFrame||a.oRequestAnimationFrame||a.msRequestAnimationFrame||function(a){b(a,1e3/60)}}]),angular.module("duScroll.scrollPosition",["duScroll.requestAnimation"]).factory("scrollPosition",["$document","$window","requestAnimation",function(a,b,c){var d=[],e=0,f=0,g=function(){f=e;for(var a=0;ai&&b(k)};k()}}function e(b,c,e){d(a.scrollX+(b||0),a.scrollY+(c||0),e)}return{scrollTo:d,scrollDelta:e}}]),angular.module("duScroll.smoothScroll",["duScroll.scroller"]).directive("duSmoothScroll",["scroller",function(a){return{link:function(b,c,d){var e=angular.element(c[0]);e.on("click",function(b){if(d.href&&-1!==d.href.indexOf("#")){var c=document.getElementById(d.href.replace(/.*(?=#[^\s]+$)/,"").substring(1));if(c){b.stopPropagation&&b.stopPropagation(),b.preventDefault&&b.preventDefault();var e=-(d.offset?parseInt(d.offset,10):0),f=c.getBoundingClientRect();f.top,a.scrollDelta(0,f.top+(isNaN(e)?0:e),1e3)}}})}}}]),angular.module("duScroll.scrollspy",["duScroll.scrollPosition"]).directive("duScrollspy",["scrollPosition",function(a){function b(){for(var a,b,c,f=0;fj&&b(l)};l()}}function f(a,b,d){e(c.x()+(a||0),c.y()+(b||0),d)}return{scrollTo:e,scrollDelta:f}}]),angular.module("duScroll.smoothScroll",["duScroll.scroller"]).directive("duSmoothScroll",["scroller",function(a){return{link:function(b,c,d){var e=angular.element(c[0]);e.on("click",function(b){if(d.href&&-1!==d.href.indexOf("#")){var c=document.getElementById(d.href.replace(/.*(?=#[^\s]+$)/,"").substring(1));if(c&&c.getBoundingClientRect){b.stopPropagation&&b.stopPropagation(),b.preventDefault&&b.preventDefault();var e=-(d.offset?parseInt(d.offset,10):0),f=c.getBoundingClientRect();a.scrollDelta(0,f.top+(isNaN(e)?0:e),1e3)}}})}}}]),angular.module("duScroll.scrollspy",["duScroll.scrollPosition"]).directive("duScrollspy",["scrollPosition",function(a){function b(){for(var a,b,c,f=0;f + Angular Scrollspy Demo