I wrote a script for binding the "smooth scroll" clicking on a button using the JavaScript Module Pattern.
Since is the first time I write code on module pattern, I need some help on the use of "this".
When I bind the "scroll" function to my "bindevents" function, I get an error saying that the "this", in the "scroll" function, is undefined.
How should i use "this" to select the button I click?
Here is the code:
var s, SmoothScroll = {
Selectors: {
Link: $('a[href*="#"]:not([href="#"])')
},
scroll: function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname && $(".classes .section").has(this).length == 0 ) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
},
bindEvents: function() {
s.Link.click(function() {
SmoothScroll.scroll();
});
},
init: function(){
s = this.Selectors;
this.bindEvents();
}
};
SmoothScroll.init();