I've managed to implement a directive that changes a HTML class element when you scroll past a certain point on the page. However, the code is a bit of a mystery to me as I pieced it together from various online sources. I really want to understand how and why it works so that I can apply similar techniques to more significant parts of my project. If anyone could shed some light on this, I would greatly appreciate it. Here is the Angular section:
myApp.directive('changeClassOnScroll', function ($window) {
return {
restrict: 'A', // What is the purpose of this line?
scope: {
offset: "@", // I'm a bit confused here
scrollClass: "@" // I'm a bit confused here
},
link: function(scope, element) {
angular.element($window).bind("scroll", function() { // I understand this
if (this.pageYOffset >= parseInt(scope.offset)) { // I understand this
element.removeClass(scope.scrollClass); // I understand this
console.log('Scrolled below header.');
} else {
element.addClass(scope.scrollClass); // I understand this
}
});
}
};
})
In the HTML;
<nav change-class-on-scroll offset="100" scroll-class="navbar-transparent" class="navbar">
<!-- I don't quite grasp why this works since the names of these elements
<!-- are different from the ones in the function above? How does the directive
<!-- know to look for 'scroll-class' even though in the directive it is
<!-- 'scrollClass' ?
Any assistance in comprehending this would be highly valued.