Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. var myApp = angular.module('myApp', ['sticky']);
  2.  
  3. function IndexCtrl($scope, $location, $anchorScroll) {
  4. $scope.gotoDiv = function (id) {
  5. $scope.id = id;
  6. $location.hash(id);
  7. $anchorScroll();
  8. }
  9. }
  10.  
  11. angular.module('sticky', [])
  12. .directive('sticky', [ function () {
  13. return {
  14. restrict: 'A',
  15.  
  16. link: function ($scope, $elem, $attrs) {
  17. var offsetTop = 0,
  18. $window = angular.element(window),
  19. initialPositionStyle = $elem.css('position'),
  20. stickyLine,
  21. scrollTop;
  22.  
  23. // Set the top offset
  24. $elem.css('top', '0');
  25. $window.on('scroll', checkSticky);
  26. setInitial();
  27.  
  28. function setInitial() {
  29. stickyLine = $elem[0].offsetTop;
  30. checkSticky();
  31. }
  32.  
  33. function checkSticky() {
  34. scrollTop = window.pageYOffset;
  35. if (scrollTop >= stickyLine) {
  36. $elem.css('position', 'fixed');
  37. } else {
  38. $elem.css('position', initialPositionStyle);
  39. }
  40. }
  41. }
  42. };
  43. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement