My first plugin done with a proper architecture, but I’m stuck at how to apply an event listener to $(window).scroll to pin the globalMessage to the top of the window. The full plugin in progress can be seen here: https://gist.github.com/937792, but the pertinent bits are the init, below.
What is the best way to set up a window event listener that modifies a css property of the target element?
(function($){
var methods = {
init: function(options) {
var $this = this;
var opts = $.extend({}, $.fn.globalMessage.defaults, options);
var data = $this.data('globalMessage');
// init global data
if ( ! data ) {
$this.data('globalMessage', {
settings : opts
});
$(window).bind("scroll.globalMessage", function() {
// ----------
// HOW TO ACCESS both $this (defined outside this context)
// and the scrollTop value to change top css val?
//-----------
$this.css("top", $(window).scrollTop());
});
$this.bind('click.globalMessage', methods.hide);
}
return $this;
},
...[other funcs]...
}
...[main entry point etc]...
})(jQuery);
Passing vars in right way to events: