Rajat Saxena
Rajat Saxena

Reputation: 3915

Why is the plugin not working on all elements except the first one

I created this image resizing plugin last night.The problem is if there is only one 'div.content',I can do the following to make it work:

window.onload=function(){
     $('div.content').image_resizer();
}

But when I add few more such 'div.content' and do:

window.onload=function(){
         $.each($('div.content'),function(k,v){
           $(this).image_resizer();
         });
}

The plugin works on the very first div.content and fail for rest of 'div.content'.Can you tell me what I am doing wrong?

[EDIT] here is the markup:

<div class="content">
        <img src="./images/img5.jpg" id='p1'/>
</div>

[EDIT 2] One more thing,when I feed $.each($('div.content'),function(k,v){$(this).image_resizer()}); manually into chrome's console.All the images resize themselves to fit into their respective divs.

[Plugin Code]

(function($){
    $.fn.image_resizer = function(options){
        $.fn.image_resizer.defaults = { MAX_HEIGHT : 280,MAX_WIDTH : 330,BORDER_WIDTH : 10 }
        var opts = $.extend({},$.fn.image_resizer.defaults,options);
        return this.each(function(){
            //plugin code starts here

            // getting array of all the images inside 'di$(this)' on which this plugin is applied
            var arr =  $(this).find('img');

            //iterating o$(this)er the array 'arr'
            $.each(arr,function(k){
                //console.log(k,$(this))
                // now resizing the image

                img_hit=$(this).height();
                img_wit=$(this).width();
                //console.log(img_hit,img_wit);
                //calculating image height to width ratio
                var ratio = img_hit/img_wit;
                if(img_wit>img_hit){
                    //console.log("wit module");
                    if(img_wit>opts.MAX_WIDTH){
                        //console.log("Image needs resizing");
                        new_wit=opts.MAX_WIDTH-(opts.BORDER_WIDTH*2);
                        $(this).width(new_wit);
                        //console.log(new_wit);
                        var new_hit = ratio*new_wit;
                        $(this).height(new_hit)
                        var space = opts.MAX_HEIGHT-(new_hit+(opts.BORDER_WIDTH*2));
                        //console.log(space);
                        $(this).css('margin-top',space/2);
                        $(this).css('margin-bottom',space/2);
                    }else{
                        //console.log("Image doesn't need resizing")
                        var space = opts.MAX_HEIGHT-(img_hit+(opts.BORDER_WIDTH*2));
                        //console.log(space);
                        $(this).css('margin-top',space/2);
                        $(this).css('margin-bottom',space/2);
                    }
                }else{
                    //console.log("hit module");
                    if(img_hit>opts.MAX_HEIGHT){
                        //console.log("Image needs resizing");
                        new_hit = opts.MAX_HEIGHT-(opts.BORDER_WIDTH*2);
                        $(this).height(new_hit);
                        var new_wit = new_hit/ratio;
                        $(this).width(new_wit);
                    }else{
                        //console.log("Image doesn't need resizing");
                        var space = opts.MAX_HEIGHT-(img_hit+(opts.BORDER_WIDTH*2));
                        //console.log(space);
                        $(this).css('margin-top',space/2);
                        $(this).css('margin-bottom',space/2);
                    }
                }

            });

            //plugin code ends here
        });
    }
})(jQuery);

Upvotes: 0

Views: 71

Answers (1)

Brandon
Brandon

Reputation: 3494

It works fine here on jsFiddle

This suggests a markup error somewhere.

As pointed out by others, there is no need to iterate. You can simply just use: $('div.content').image_resizer();

Upvotes: 1

Related Questions