Get the real width and height of an image with JavaScript? (in Safari/Chrome)

后端 未结 30 2382
傲寒
傲寒 2020-11-22 01:16

I am creating a jQuery plugin.

How do I get the real image width and height with Javascript in Safari?

The following works with Firefox 3, IE7 and Opera 9:

30条回答
  •  悲哀的现实
    2020-11-22 01:47

    Stumbled upon this thread trying to find an answer for my own question. I was trying to get an image's width/height in a function AFTER the loader, and kept coming up with 0. I feel like this might be what you're looking for, though, as it works for me:

    tempObject.image = $('').attr({ 'src':"images/prod-" + tempObject.id + ".png", load:preloader });
    xmlProjectInfo.push(tempObject);
    
    function preloader() {
        imagesLoaded++;
        if (imagesLoaded >= itemsToLoad) { //itemsToLoad gets set elsewhere in code
            DetachEvent(this, 'load', preloader); //function that removes event listener
            drawItems();
        }   
    }
    
    function drawItems() {
        for(var i = 1; i <= xmlProjectInfo.length; i++)
            alert(xmlProjectInfo[i - 1].image[0].width);
    }
    

提交回复
热议问题