Skip to content

Commit 7fba09d

Browse files
committed
Added sticky nav
1 parent 8ecd2be commit 7fba09d

File tree

6 files changed

+121
-1
lines changed

6 files changed

+121
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
/node_modules

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"Dan Mindru <mindrudan@gmail.com>"
77
],
88
"description": "Angular directive to make a sticky element, read more on https://ngmilk.rocks/2015/04/09/angularjs-sticky-navigation-directive/",
9-
"main": "ng-sticky.js",
9+
"main": "dist/ng-sticky.js",
1010
"keywords": [
1111
"angular",
1212
"angularjs",

dist/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
var del = require('del'),
4+
gulp = require('gulp'),
5+
gulpsync = require('gulp-sync')(gulp),
6+
uglify = require('gulp-uglify'),
7+
eslint = require('gulp-eslint');
8+
9+
var options = {
10+
src: 'src',
11+
dist: 'dist'
12+
};
13+
14+
gulp.task('scripts', function () {
15+
return gulp.src([options.src + '/**/*.js'])
16+
.pipe(eslint())
17+
.pipe(uglify())
18+
.pipe(gulp.dest(options.dist));
19+
});
20+
21+
gulp.task('clean', function (cb){
22+
return del([options.dist], cb);
23+
});
24+
25+
gulp.task('watch', function () {
26+
return gulp.watch([options.src + '/**/*'], ['build']);
27+
});
28+
29+
gulp.task('default', gulpsync.sync([['build'], ['watch']]));
30+
gulp.task('build', gulpsync.sync([['clean'], ['scripts']]));

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "angular-sticky-navigation-directive",
3+
"version": "0.0.1",
4+
"description": "Angular directive to make a sticky element, read more on https://ngmilk.rocks/2015/04/09/angularjs-sticky-navigation-directive/",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/ng-milk/angular-sticky-navigation-directive.git"
12+
},
13+
"keywords": [
14+
"angular",
15+
"angularjs",
16+
"directive",
17+
"sticky",
18+
"navigation"
19+
],
20+
"author": "Dan Mindru <mindrudan@gmail.com> (http://mindrudan.com/)",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/ng-milk/angular-sticky-navigation-directive/issues"
24+
},
25+
"homepage": "https://github.com/ng-milk/angular-sticky-navigation-directive#readme"
26+
}

src/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* angular-sticky-navigation-directive v0.0.1
3+
* (c) 2015 Dan Mindru http://mindrudan.com
4+
* License: MIT
5+
*/
6+
7+
angular.module('dm.stickyNav', [])
8+
.directive('sticky-nav', stickyNavDirective);
9+
10+
stickyNavDirective.$inject = ['$window'];
11+
function stickyNavDirective($window){
12+
function stickyNavLink(scope, element){
13+
var w = angular.element($window),
14+
size = element[0].clientHeight,
15+
top = 0;
16+
17+
/*
18+
* On scroll we just check the page offset
19+
* if it's bigger than the target size we fix the controls
20+
* otherwise we display them inline
21+
*/
22+
function toggleStickyNav(){
23+
if(!element.hasClass('controls-fixed') && $window.pageYOffset > top + size){
24+
element.addClass('controls-fixed');
25+
} else if(element.hasClass('controls-fixed') && $window.pageYOffset <= top + size){
26+
element.removeClass('controls-fixed');
27+
}
28+
}
29+
30+
/*
31+
* We update the top position -> this is for initial page load,
32+
* while elements load
33+
*/
34+
scope.$watch(function(){
35+
return element[0].getBoundingClientRect().top + $window.pageYOffset;
36+
}, function(newValue, oldValue){
37+
if(newValue !== oldValue && !element.hasClass('controls-fixed')){
38+
top = newValue;
39+
}
40+
});
41+
42+
/*
43+
* Resizing the window displays the controls inline by default.
44+
* This is needed to calculate the correct boundingClientRect.
45+
* After the top is updated we toggle the nav, eventually
46+
* fixing the controls again if needed.
47+
*/
48+
w.bind('resize', function stickyNavResize(){
49+
element.removeClass('controls-fixed');
50+
top = element[0].getBoundingClientRect().top + $window.pageYOffset;
51+
toggleStickyNav();
52+
});
53+
w.bind('scroll', toggleStickyNav);
54+
}
55+
56+
return {
57+
scope: {},
58+
restrict: 'A',
59+
link: stickyNavLink
60+
};
61+
}

0 commit comments

Comments
 (0)