Get actual image size, after resizing it

后端 未结 3 629
春和景丽
春和景丽 2021-02-05 11:01

I have a page filled with thumbnails, resized with css to 150x150, and when I click on a thumbnail, the page dims, and The image is being shown in it\'s true sizes.

3条回答
  •  眼角桃花
    2021-02-05 11:26

    One way you could do it is create a separate image object.

    function getImageDimensions(path,callback){
        var img = new Image();
        img.onload = function(){
            callback({
                width : img.width,
                height : img.height
            });
        }
        img.src = path;
    }
    
    getImageDimensions('image_src',function(data){
        var img = data;
    
        //img.width
        //img.height
    
    });
    

    That way, you'll use the same image but not the one on the DOM, which has modified dimensions. Cached images, as far as i know, will be recycled using this method. So no worries about additional HTTP requests.

提交回复
热议问题