Plugin with multiple CSS selectors
Hi!
I made this little plugin to make elements grow to match there container size.
It looks like this:
- (function($){
- $.fn.spaceWidths = function(options) {
- var parent_width = $(this).parent().width();
- var childs = 0;
- var child_count = $(this).size();
- this.each(function(){
- childs = childs + $(this).width();
- });
-
- var difference = parent_width - childs;
- var add_padding = Math.floor(difference / child_count / 2);
-
- return this.each(function(){
- $(this).css({
- 'padding-right': add_padding,
- 'padding-left': add_padding
- });
- });
-
- };
- })(jQuery);
This works great, when I'm only using it for 1 element on a page.
Let's say I've got the following:
- $("ul.list li").spaceWidths();
- <ul class="list">
- <li>Hello</li>
- <li>There</li>
- </ul>
The plugin now counts 2 childs.
This works, but If I add another list:
- $("ul.list li").spaceWidths();
- <ul class="list">
- <li>Hello</li>
- <li>There</li>
- </ul>
- <ul class="list">
- <li>This will</li>
- <li>not work</li>
- </ul>
The plugin now counts 4 childs.
This is incorrect, because it should do this for selected element separately.
Anyone who can help me with this =)
Thanks!