jquery way to limit image size.

| 2 Comments | No TrackBacks

UPDATE: You might be interested in reading this article: Javascript's way to know the image size.


Find all images under current box, and limit their width to at most the same as current box.

        var w = $(this).width();
        $(this).find("img").each(function() {
            if ( $(this).width() > w ) {
                $(this).width("100%");
            }
        });

This is something similar to always wrap a div around an image. Only it's a piece of un-obstructive javascript instead of over-augmented-semantic-hack-sorting-out-bad-css-box-model.

This generally works good on Firefox, IE7. Didn't try others.

Labels: , ,

No TrackBacks

TrackBack URL: http://pub.gugod.org/mt/mt-tb.cgi/33

2 Comments

Do you just insert this (copy/paste) into any HTML? I need to shrink my image size in the sidebar of my blog or limit size. http://thebloggingbunny.blogspot.comthe widget for the "follow me" tumbler inclusion is javascript, and can be altered/skined with CSS...supposedly, but all the tricks I try end up not working. :(
------

Bunny,The behavior really depends on the value of first "this" variable.I suggest you to try this for your website instead:$("#sidebar-wrapper img").each(function() { $(this).width("200px");});However, you'll need to load jQuery in your blog first.
------


--------

Leave a comment