ngScrollTo
ngScrollTo animation example (w/ jQuery + jQuery scrollintoview plugin)
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<div ng-app="myApp">
<p>Top</p>
<div>
<a scroll-to="section1">Goto Section 1</a>
</div>
<div style="padding-top:1000px">...</div>
<div id="section1">
<h2>Section1</h2>
<a scroll-to="">Back to Top</a>
</div>
</div>
<script>
angular.module("myApp", ["ngScrollTo"])
.config(function(ngScrollToOptionsProvider) {
ngScrollToOptionsProvider.extend({
handler: function(el) {
$(el).scrollintoview();
}
});
});
</script>
<script>
/*
* jQuery scrollintoview() plugin and :scrollable selector filter
*
* Version 1.8 (14 Jul 2011)
* Requires jQuery 1.4 or newer
*
* Copyright (c) 2011 Robert Koritnik
* Licensed under the terms of the MIT license
* http://www.opensource.org/licenses/mit-license.php
*/
(function(f){var c={vertical:{x:false,y:true},horizontal:{x:true,y:false},both:{x:true,y:true},x:{x:true,y:false},y:{x:false,y:true}};var b={duration:"fast",direction:"both"};var e=/^(?:html)$/i;var g=function(k,j){j=j||(document.defaultView&&document.defaultView.getComputedStyle?document.defaultView.getComputedStyle(k,null):k.currentStyle);var i=document.defaultView&&document.defaultView.getComputedStyle?true:false;var h={top:(parseFloat(i?j.borderTopWidth:f.css(k,"borderTopWidth"))||0),left:(parseFloat(i?j.borderLeftWidth:f.css(k,"borderLeftWidth"))||0),bottom:(parseFloat(i?j.borderBottomWidth:f.css(k,"borderBottomWidth"))||0),right:(parseFloat(i?j.borderRightWidth:f.css(k,"borderRightWidth"))||0)};return{top:h.top,left:h.left,bottom:h.bottom,right:h.right,vertical:h.top+h.bottom,horizontal:h.left+h.right}};var d=function(h){var j=f(window);var i=e.test(h[0].nodeName);return{border:i?{top:0,left:0,bottom:0,right:0}:g(h[0]),scroll:{top:(i?j:h).scrollTop(),left:(i?j:h).scrollLeft()},scrollbar:{right:i?0:h.innerWidth()-h[0].clientWidth,bottom:i?0:h.innerHeight()-h[0].clientHeight},rect:(function(){var...
JavaScript
// Version 0.0.7
// AngularJS simple hash-tag scroll alternative
// this directive uses click event to scroll to the target element
//
// <div ng-app="app">
// <div ng-controller="myCtrl">
// <a scroll-to="section1">Section 1</a>
// </div>
// ...
// <div id="section1">
// <h2>Section1</h2>
// <a scroll-to="">Back to Top</a>
// </div>
// ...
// <div id="section1">
// <h2>Section1</h2>
// <a scroll-to="section1" offset="60">Section 1 with 60px offset</a>
// </div>
// </div>
angular.module('ngScrollTo', []);
angular.module('ngScrollTo')
.directive('scrollTo', ['ScrollTo', function(ScrollTo){
return {
restrict : "AC",
compile : function(){
return function(scope, element, attr) {
var handler = function (event) {
event.preventDefault();
ScrollTo.idOrName(attr.scrollTo, attr.offset);
};
element.bind("click", handler);
scope.$on('$destroy', function () {
element.unbind("click", handler);
});
};
}
};
}])
.service('ScrollTo', ['$window', 'ngScrollToOptions', function($window, ngScrollToOptions) {
this.idOrName = function (idOrName, offset, focus) {//find element with the given id or name and scroll to the first element it finds
var currentDocument = $window.document;
if(!idOrName) {//move to top if idOrName is not provided
$window.scrollTo(0, 0);
}
if(focus === undefined) { //set default action to focus element
focus = true;
}
//check if an element can be found with id attribute
var el = currentDocument.getElementById(idOrName);
if(!el) {//check if an element can be found with name attribute if there is no such id
el = currentDocument.getElementsByName(idOrName);
if(el && el.length)
el = el[0];
else
el = null;
}
...