Get height of image inside a hidden div

前端 未结 9 620
自闭症患者
自闭症患者 2021-01-02 18:50

i want to to get the height/width of an image inside a hidden div-containter, but .height() and .width() both returns 0 (like expected).

         


        
9条回答
  •  时光取名叫无心
    2021-01-02 19:16

    Yes its because the image is not loaded yet and this is my solution:

    $('').appendTo('body').css({
                    'position': 'absolute',
                    'top': -1
                }).load(function() {
                    console.log('Image width: ' + $(this).width());
                    console.log('Image height: ' + $(this).height());
                    $(this).remove();
                });
    

提交回复
热议问题