-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed body class on destroy, v1.7.6
- Loading branch information
Showing
8 changed files
with
256 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; | ||
}()); | ||
|
||
}()); |
Oops, something went wrong.