Skip to content

Commit

Permalink
Removed body class on destroy, v1.7.6
Browse files Browse the repository at this point in the history
  • Loading branch information
d-oliveros committed Jan 12, 2015
2 parents aeddd44 + 89f82fa commit 9d25366
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 194 deletions.
14 changes: 14 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"eqeqeq": true,
"newcap": true,
"nonew": true,
"undef": true,
"unused": true,
"freeze": true,
"globals": {
"window": false,
"document": false,
"console": false,
"angular": false
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 David Oliveros
Copyright (c) 2015 David Oliveros

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "angular-sticky",
"version": "1.7.5",
"version": "1.7.6",
"homepage": "https://github.com/d-oliveros/angular-sticky",
"authors": [
"David Oliveros <[email protected]>"
],
"description": "A simple, pure javascript (No jQuery required!) AngularJS directive to make elements stick when scrolling down.",
"main": "sticky.js",
"main": "lib/sticky.js",
"dependencies": {
"angular": "^1.2.0",
"matchmedia": "~0.2.0"
Expand Down
1 change: 1 addition & 0 deletions dist/sticky.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

216 changes: 216 additions & 0 deletions lib/sticky.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
(function () {
'use strict';

var module = angular.module('sticky', []);

// Directive: sticky
//
module.directive('sticky', function() {
return {
restrict: 'A', // this directive can only be used as an attribute.
link: linkFn
};

function linkFn($scope, $elem, $attrs) {
var mediaQuery, stickyClass, bodyClass, elem, $window, $body,
doc, initialCSS, initialStyle, isPositionFixed, isSticking,
stickyLine, offset, anchor, prevOffset, matchMedia;

isPositionFixed = false;
isSticking = false;

matchMedia = window.matchMedia;

// elements
$window = angular.element(window);
$body = angular.element(document.body);
elem = $elem[0];
doc = document.documentElement;

// attributes
mediaQuery = $attrs.mediaQuery || false;
stickyClass = $attrs.stickyClass || '';
bodyClass = $attrs.bodyClass || '';

initialStyle = $elem.attr('style');

offset = typeof $attrs.offset === 'string' ?
parseInt($attrs.offset.replace(/px;?/, '')) :
0;

anchor = typeof $attrs.anchor === 'string' ?
$attrs.anchor.toLowerCase().trim()
: 'top';

// initial style
initialCSS = {
top: $elem.css('top'),
width: $elem.css('width'),
position: $elem.css('position'),
marginTop: $elem.css('margin-top'),
cssLeft: $elem.css('left')
};

switch (anchor) {
case 'top':
case 'bottom':
break;
default:
console.log('Unknown anchor '+anchor+', defaulting to top');
anchor = 'top';
break;
}


// Listeners
//
$window.on('scroll', checkIfShouldStick);
$window.on('resize', $scope.$apply.bind($scope, onResize));
$scope.$on('$destroy', onDestroy);

function onResize() {
initialCSS.offsetWidth = elem.offsetWidth;
}

function onDestroy() {
$window.off('scroll', checkIfShouldStick);
$window.off('resize', onResize);

if ( bodyClass ) {
$body.removeClass(bodyClass);
}
}


// Watcher
//
prevOffset = _getTopOffset(elem);

$scope.$watch( function() { // triggered on load and on digest cycle
if ( isSticking ) return prevOffset;

prevOffset =
(anchor === 'top') ?
_getTopOffset(elem) :
_getBottomOffset(elem);

return prevOffset;

}, function(newVal, oldVal) {
if ( newVal !== oldVal || typeof stickyLine === 'undefined' ) {
stickyLine = newVal - offset;
checkIfShouldStick();
}
});


// Methods
//
function checkIfShouldStick() {
var scrollTop, shouldStick, scrollBottom, scrolledDistance;

if ( mediaQuery && !matchMedia('('+mediaQuery+')').matches)
return;

if ( anchor === 'top' ) {
scrolledDistance = window.pageYOffset || doc.scrollTop;
scrollTop = scrolledDistance - (doc.clientTop || 0);
shouldStick = scrollTop >= stickyLine;
} else {
scrollBottom = window.pageYOffset + window.innerHeight;
shouldStick = scrollBottom <= stickyLine;
}

// Switch the sticky mode if the element crosses the sticky line
if ( shouldStick && !isSticking )
stickElement();

else if ( !shouldStick && isSticking )
unstickElement();
}

function stickElement() {
var rect, absoluteLeft;

rect = $elem[0].getBoundingClientRect();
absoluteLeft = rect.left;

initialCSS.offsetWidth = elem.offsetWidth;

isSticking = true;

if ( bodyClass ) {
$body.addClass(bodyClass);
$elem.addClass(stickyClass);
}

$elem
.css('width', elem.offsetWidth+'px')
.css('position', 'fixed')
.css(anchor, offset+'px')
.css('margin-top', 0);

if ( anchor === 'bottom' ) {
$elem.css('margin-bottom', 0);
}
}

function unstickElement() {
//$elem[0].removeAttribute("style");
$elem.attr('style', $elem.initialStyle);
isSticking = false;

if ( bodyClass ) {
$body.removeClass(bodyClass);
}

if ( stickyClass ) {
$elem.removeClass(stickyClass);
}

$elem
.css('width', initialCSS.offsetWidth+'px')
.css('top', initialCSS.top)
.css('position', initialCSS.position)
.css('margin-top', initialCSS.marginTop);
}

function _getTopOffset (element) {
var pixels = 0;

if (element.offsetParent) {
do {
pixels += element.offsetTop;
element = element.offsetParent;
} while (element);
}

return pixels;
}

function _getBottomOffset (element) {
return element.offsetTop + element.clientHeight;
}
}

});

// Shiv: matchMedia
//
window.matchMedia = window.matchMedia || (function() {
var warning = 'angular-sticky: This browser does not support '+
'matchMedia, therefore the minWidth option will not work on '+
'this browser. Polyfill matchMedia to fix this issue.';

if ( window.console && console.warn ) {
console.warn(warning);
}

return function() {
return {
matches: true
};
};
}());

}());
Loading

0 comments on commit 9d25366

Please sign in to comment.