diff --git a/.gitignore b/.gitignore index a72b52e..d03b49a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ results npm-debug.log node_modules + +.idea \ No newline at end of file diff --git a/README.md b/README.md index c6b3364..e682a8e 100644 --- a/README.md +++ b/README.md @@ -58,3 +58,30 @@ Length of time, in milliseconds, the animation will take. Default value: `html, body` The container element to scroll. + +## Config + +You can globally configure the options by using the scrollToConfigProvider: + +``` +(function () { + 'use strict'; + + angular.module('app.core') + .config(configure); + + /* @ngInject */ + function configure(scrollToConfigProvider) { + scrollToConfigProvider.config.offset = 80; + scrollToConfigProvider.config.duration = 200; + scrollToConfigProvider.config.container = 'body'; + } +})(); +``` + +### [UPDATED 2014-05-21] Included hook allow the scrolling action to be triggered from a controller + +Added a `triggerScroll(delay)` function that will trigger the scroll action after an optional delay (in milliseconds) + + + diff --git a/angular-scrollto.js b/angular-scrollto.js index 3ee53e4..25f5f76 100644 --- a/angular-scrollto.js +++ b/angular-scrollto.js @@ -1,33 +1,43 @@ -angular.module('scrollto', []); +(function () { + 'use strict'; + + angular.module('scrollto', []) + .provider('scrollToConfig', scrollToConfig) + .directive('scrollTo', scrollTo); + + function scrollToConfig() { + this.config = {container: 'html, body', offset: 0, duration: 150}; + this.$get = function () { return {config: this.config}; }; + } + + scrollTo.$inject = ['$timeout', 'scrollToConfig']; + + function scrollTo($timeout, scrollToConfig) { + return {restrict: 'A', link: link}; + + function link(scope, element, attrs) { + var settings = angular.extend( + {scrollTo: angular.element(), easing: 'swing'}, + scrollToConfig.config, attrs); + element.on('click', function () { $timeout(scroll(settings)); }); + + // Hook to trigger the scrolling action outside of the target element (e.g. can call from a Controller) + scope.triggerScroll = function(delay) { + delay = delay || 0; // delay is optional + $timeout(scroll(settings), delay); + }; + } -angular.module('scrollto') - .directive('scrollTo', ['$timeout', function ($timeout) { - function scroll (settings) { return function () { var scrollPane = angular.element(settings.container); var scrollTo = (typeof settings.scrollTo == "number") ? settings.scrollTo : angular.element(settings.scrollTo); + if (typeof scrollTo.offset() == 'undefined') { return; } // element no longer available var scrollY = (typeof scrollTo == "number") ? scrollTo : scrollTo.offset().top - settings.offset; scrollPane.animate({scrollTop : scrollY }, settings.duration, settings.easing, function(){ if (typeof callback == 'function') { callback.call(this); } }); } } - - return { - restrict: 'A', - link: function (scope, element, attrs) { - var settings = angular.extend({ - container: 'html, body', - scrollTo: angular.element(), - offset: 0, - duration: 150, - easing: 'swing' - }, attrs); - - element.on('click', function () { - $timeout(scroll(settings)); - }); - } - }; - }]); \ No newline at end of file + } +})(); \ No newline at end of file diff --git a/angular-scrollto.min.js b/angular-scrollto.min.js index fd3374e..3141e9d 100644 --- a/angular-scrollto.min.js +++ b/angular-scrollto.min.js @@ -1 +1 @@ -angular.module("scrollto",[]),angular.module("scrollto").directive("scrollTo",["$timeout",function(a){function b(a){return function(){var b=angular.element(a.container),c="number"==typeof a.scrollTo?a.scrollTo:angular.element(a.scrollTo),d="number"==typeof c?c:c.offset().top-a.offset;b.animate({scrollTop:d},a.duration,a.easing,function(){"function"==typeof callback&&callback.call(this)})}}return{restrict:"A",link:function(c,d,e){var f=angular.extend({container:"html, body",scrollTo:angular.element(),offset:0,duration:150,easing:"swing"},e);d.on("click",function(){a(b(f))})}}}]); \ No newline at end of file +!function(){"use strict";function a(){this.config={container:"html, body",offset:0,duration:150},this.$get=function(){return{config:this.config}}}function b(a,b){function c(c,e,f){var g=angular.extend({scrollTo:angular.element(),easing:"swing"},b.config,f);e.on("click",function(){a(d(g))}),c.triggerScroll=function(b){b=b||0,a(d(g),b)}}function d(a){return function(){var b=angular.element(a.container),c="number"==typeof a.scrollTo?a.scrollTo:angular.element(a.scrollTo);if("undefined"!=typeof c.offset()){var d="number"==typeof c?c:c.offset().top-a.offset;b.animate({scrollTop:d},a.duration,a.easing,function(){"function"==typeof callback&&callback.call(this)})}}}return{restrict:"A",link:c}}angular.module("scrollto",[]).provider("scrollToConfig",a).directive("scrollTo",b),b.$inject=["$timeout","scrollToConfig"]}(); \ No newline at end of file diff --git a/bower.json b/bower.json index 82701eb..e8b3a0c 100644 --- a/bower.json +++ b/bower.json @@ -1,9 +1,11 @@ { "name": "angular-scrollto", - "version": "0.1.4", - "homepage": "https://github.com/scottcorgan/angular-scrollto", + "version": "0.1.9", + "homepage": "https://github.com/mkirchstein/angular-scrollto", "authors": [ - "Scott Corgan " + "Scott Corgan ", + "Matt Kirchstein ", + "Dieter Geerts " ], "description": "Angular directive to scroll to element by selector", "main": "angular-scrollto.js", diff --git a/package.json b/package.json index 6425ca1..821f392 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-scrollto", - "version": "0.1.4", + "version": "0.1.9", "description": "Angular directive to scroll to element by selector", "main": "angular-scrollto.js", "scripts": { @@ -8,17 +8,17 @@ }, "repository": { "type": "git", - "url": "https://github.com/scottcorgan/angular-scrollto.git" + "url": "https://github.com/mkirchstein/angular-scrollto.git" }, "keywords": [ "angular", "scroll", "directive" ], - "author": "Scott Corgan", + "author": "Scott Corgan, Matt Kirchstein, Dieter Geerts", "license": "MIT", "bugs": { - "url": "https://github.com/scottcorgan/angular-scrollto/issues" + "url": "https://github.com/mkirchstein/angular-scrollto/issues" }, "devDependencies": { "grunt": "~0.4.2",