Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var myApp = angular.module('myApp', ['sticky']);
- function IndexCtrl($scope, $location, $anchorScroll) {
- $scope.gotoDiv = function (id) {
- $scope.id = id;
- $location.hash(id);
- $anchorScroll();
- }
- }
- angular.module('sticky', [])
- .directive('sticky', [ function () {
- return {
- restrict: 'A',
- link: function ($scope, $elem, $attrs) {
- var offsetTop = 0,
- $window = angular.element(window),
- initialPositionStyle = $elem.css('position'),
- stickyLine,
- scrollTop;
- // Set the top offset
- $elem.css('top', '0');
- $window.on('scroll', checkSticky);
- setInitial();
- function setInitial() {
- stickyLine = $elem[0].offsetTop;
- checkSticky();
- }
- function checkSticky() {
- scrollTop = window.pageYOffset;
- if (scrollTop >= stickyLine) {
- $elem.css('position', 'fixed');
- } else {
- $elem.css('position', initialPositionStyle);
- }
- }
- }
- };
- }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement