I'm trying to develop my first jQuery plugin. Basically it appends classes to various elements on site an then changes it when user scrolls (I'm calculating offsets and whatnot). And I think I've hit a wall with this one.
Here's how I start the plugin:
$("div").myPlugin();
And the source:
$.fn.myPlugin = function() {
return this.each(function() {
$(this).addClass("something-" + i);
i++;
});
};
Could someone explain please how to use $(window).scroll in my plugin?
Because once it into "return this.each" it gets attached as many times as many elements there are in this loop...
$.fn.myPlugin = function() {
return this.each(function() {
$(this).addClass("something-" + i);
i++;
$(window).scroll( function() { <!-- here it not only attaches itself x-times but i is always the same -->
...
$(".something-" + i).addClass("active");
...
});
});
};
Putting it after return doesn't do nothing:
$.fn.myPlugin = function() {
return this.each(function() {
$(this).addClass("something-" + i);
i++;
});
$(window).scroll( function() { <!-- doesn't seem to work -->
...
$(".something-" + i).addClass("active");
...
});
};
And before there are no "i"s, also I don't know if I can put anything outside of the "return" scope inside a function (doesn't seem valid to me?):
$.fn.myPlugin = function() {
$(window).scroll( function() { <!-- there is no "i" yet -->
...
$(".something-" + i).addClass("active");
...
});
return this.each(function() {
$(this).addClass("something-" + i);
i++;
});
};
I'm new to jQuery and I'm not sure if I understand the documentation correctly, I was wondering wether it might be better to do not use return here at all? Note this plugin can work with 1 element but usually there will be more divs than 1.
Thanks a lot.
There are probably several ways to write the plugin. Here are the characteristics of what seems most appropriate :
this
- no need for.each()
(at least, not overtly).Please note that these characteristics, when coded, will be in a very atypical plugin pattern. Therefore, this is probably not the best problem to choose for your first plugin.
However, here goes ...
Associate selected elements as follows :
Dissociate selected elements as follows :
DEMO
Note that I changed the class name to
pluginName + 'active'
to make it far less likely to be used by some other piece of code.As I said, that's one way to do it. Maybe you can think of improvements or some other way entirely.
How about this:
http://jsfiddle.net/yw0q5hn7/2/