Created
June 17, 2015 21:38
-
-
Save victormejia/73ddd945c52fa8d7efd4 to your computer and use it in GitHub Desktop.
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
.directive('scrollPosition', ['$window', function($window) { | |
return { | |
scope: { | |
target: '=scrollPosition' | |
}, | |
link: function (scope, element) { | |
// Detect request animation frame | |
var scroll = window.requestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.mozRequestAnimationFrame || | |
window.msRequestAnimationFrame || | |
window.oRequestAnimationFrame || | |
// IE Fallback, you can even fallback to onscroll | |
function(callback){ window.setTimeout(callback, 1000/60) }; | |
var lastPosition = -1; | |
function loop(){ | |
// Avoid calculations if not needed | |
if (lastPosition == window.pageYOffset) { | |
scroll(loop); | |
return false; | |
} | |
else { | |
lastPosition = window.pageYOffset; | |
if (lastPosition < scope.target) { | |
element.removeClass('default').addClass('transparent'); | |
} else { | |
element.removeClass('transparent').addClass('default'); | |
} | |
} | |
scroll(loop); | |
} | |
loop(); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment