Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real image width with JavaScript

Tags:

I have the next function:

function setImagesWidth(id,width) {
    var images = document.getElementById(id).getElementsByTagName("img");
    for(var i = 0; i < images.length;i++) {
        // If the real width is bigger than width parameter
            images[i].style.width=width;    
        //}         
    }       
}

I would like to set the css width attribute of all my img tags to a particular value only when the image real width is bigger than the attribute value. If it is possible, i would like a solution which does not use any particular framework.


images[i].offsetWidth returns 111 for an image of 109px width. Is this because 1px each side border?

like image 442
Sergio del Amo Avatar asked Sep 03 '08 19:09

Sergio del Amo


People also ask

How do you get the image width and height using JS?

Use imgElement. naturalWidth and imgElement. naturalHeight for getting width and height. Regardless of whether you want to use Javascript or jQuery it is essential that the image is visible in the browser.

Can you change an images size using JavaScript?

Answer: Use the JavaScript width and height property You can use either width or height JavaScript property to proportionally increase and decrease the dimension of an image like zoom-in and zoom-out feature.

How can get image width and height in jQuery?

var imageWidth = $(Imgsize). width(); alert(imageWidth); var imageHeight = Imgsize.

How do you find the width and height of an image in HTML?

You can easily find the original or intrinsic width and heigh of an image using the HTML5 image naturalWidth and naturalHeight properties. These properties are supported in all major web browsers such as Chrome, Firefox, Safari, Opera, Internet Explorer 9 and above.


2 Answers

Here is, hopefully, enough sample code to give you what you want:

var myImage = document.getElementById("myImagesId");
var imageWidth = myImage.offsetWidth;
var imageHeight = myImage.offsetHeight;

That should give you the numbers you need to derive the solution you want. I think you can write the rest of the code yourself. :)


EDIT: Here, I couldn't help myself - is this what you are after?

function setImagesWidth(id,width) {
   var images = document.getElementById(id).getElementsByTagName("img");
   for(var i = 0; i < images.length;i++) {
      if(images[i].offsetWidth > width) {
         images[i].style.width= (width + "px");
      }
   }           
}
like image 56
Jason Bunting Avatar answered Sep 30 '22 13:09

Jason Bunting


@Sergio del Amo: Indeed, if you check out my link you'll see that you want clientWidth instead.

@Sergio del Amo: You cannot, unfortunately, accept your own answer. But you do have an extraneous period in the "px" suffix, so let's go with this, including the clientWidth change:

// width in pixels
function setImagesWidth(id, width)
{
    var images = document.getElementById(id).getElementsByTagName("img");
    var newWidth = width + "px";
    for (var i = 0; i < images.length; ++i)
    {
        if (images[i].clientWidth > width)
        {
            images[i].style.width = newWidth;
        }                       
    }
}
like image 24
Domenic Avatar answered Sep 30 '22 14:09

Domenic