I am converting a jQuery plugin into an Angular directive but still not working properly or: not working at all.
This is the behavior that I want to achieve
Here is also jsfiddle
Remember that all I want to achieve with this is the behavior on the left sidebar, where itโs sticky everywhere.
I did this with jQuery (I'm new to Angular) and I need it to work with Angular. Please go to the Plunkr Example , this is the behavior I want. Thanks in advance if some of you guys take the time to help me.
Take a look at 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 that the console.log appears after scrolling.
source share