Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning jQuery into Angular, fixed sidebar not working (?)

I am converting a jQuery Plugin into an Angular Directive, but still not working properly, or: not working at all.

This is the behavior I want to achieve

Here is a jsfiddle also

Remember that all I want to achieve with this, is the behavior on the left sidebar where says 'sticky' everywhere.

I did it with jQuery (I am new to Angular) and I need to have it working with Angular. Please go and see the Plunkr Example, that behavior, is the one I want. Thanks in advance if some of you guys take the time to help me.

Look at the jQuery code:

$(function() {
    var offset = $("#sidebar").offset();
    var topPadding = 85;
    $(window).scroll(function() {
        if ($(window).scrollTop() > offset.top) {
            $("#sidebar").stop().animate({
                marginTop: $(window).scrollTop() - offset.top + topPadding
            });
        } else {
            $("#sidebar").stop().animate({
                marginTop: 50
            });
        };
    });
});

and here is my Angular Directive:

angular.module('capilleira.clickAndGambleWebApp.directives', [])
  .directive('sticky', function ($window) {
    return {
      restrict: 'A',
      controller: ($scope)
      link: function (scope, element, attrs) {
        var raw = element[0],
            offset = element.offset(),
            topPadding = 85;

        angular.element($window).bind('scroll', function () {
          console.log('SCROOOOOOOOOOOOOLLLL');
          if ($window.scrollTop > offset.top) {
            raw.stop().animate({
              marginTop: $window.scrollTop() - offset.top + topPadding
            });
          }
        });
      }
    }
  });

my directive is good at the point that once you do scroll, the console.log shows up.

like image 325
Reacting Avatar asked Jan 09 '15 21:01

Reacting


1 Answers

I have it working already my friends. This is the directive working properly

angular.module('capilleira.clickAndGambleWebApp.directives', [])
.directive('sticky', function($document) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      angular.element(document).ready(function() {
        var offset = element.offset(),
            topPadding = 85;
        $document.scroll(function() {
          if ($document.scrollTop() > offset.top) {
            element.stop().animate({
              marginTop: $document.scrollTop() - offset.top + topPadding
            });
          } else {
            element.stop().animate({
              marginTop: 0
            });
          };
        });
      });
    }
  };
});
like image 56
Reacting Avatar answered Nov 06 '22 06:11

Reacting