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.
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
});
};
});
});
}
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With