I've written an app that has a list of blocks.
Each block contains links to other blocks. When a link to a block is clicked e.g #/home/page-19, the page animates down/up depending on current position.
This all currently works but when the anchor is clicked and the route updates, the browser jumps to the top and then animates down, I need it to animate from its current position.
I've written a directive that adds preventDefault to all anchors.
See JS fiddle below. http://jsfiddle.net/ygNWF/10/
Stripped down code:
HTML:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<body ng-app="app" ng-controller="appController">
<script type="text/ng-template" id="home-template">
<div class="page" id="page-17" >
<div class="page-inner" ng-style="style()" resize><a href="#/home/page-18">Page 18</a></div>
</div>
<div class="page" id="page-18">
<div class="page-inner" ng-style="style()" resize><a href="#/home/page-19">Page 19</a></div>
</div>
<div class="page" id="page-19">
<div class="page-inner" ng-style="style()" resize><a href="#/home/page-20">Page 20</a></div>
</div>
<div class="page" id="page-20">
<div class="page-inner" ng-style="style()" resize><a href="#/home/page-17">Page 17</a></div>
</div>
</script>
<div class="wrapper">
<div class="view" ng-view scroll></div>
</div>
</body>
javascript:
var app = angular.module('app', []);
/*
Controllers
*/
app.controller( 'appController', function( $scope ) {
});
app.controller( 'routeController', function( $scope, $routeParams ) {
//When route change, check if page has been updated and run animateScroll directive
if(typeof $routeParams !== 'undefined' && $routeParams.page.length > 0){
$scope.animateScroll($routeParams.page);
}
});
/*
Page routes
*/
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/home/:page', {
templateUrl: 'home-template',
controller: 'routeController'
})
.otherwise({
redirectTo: '/home/'
});
}]);
/*
Directives
*/
app.directive('scroll', function ($routeParams, $timeout) {
/*
Scroll to element ID
*/
return {
restrict: 'A',
link: function(scope, elm, attrs) {
scope.animateScroll = function(page) {
$timeout(function(){
console.log('test');
if(jQuery('#' + page).length == 1){
jQuery('html, body').animate({
scrollTop: jQuery('#' + page).position().top
});
};
}, 1);
};
}
};
});
app.directive('resize', function ($window) {
return function (scope, element) {
var w = angular.element($window);
scope.getWindowDimensions = function () {
return { height: w.height() };
};
scope.$watch(scope.getWindowDimensions, function (dimensions) {
scope.windowHeight = dimensions.height;
scope.style = function () {
return {
'min-height': scope.windowHeight + 'px'
};
};
}, true);
w.bind('resize', function () {
scope.$apply();
});
}
});
app.directive('a', function() {
return {
restrict: 'E',
link: function(scope, elem, attrs) {
elem.on('click', function(e){
e.preventDefault();
});
}
};
});
Page layout example:
