Image width after load returns 0

∥☆過路亽.° 提交于 2019-12-11 22:31:19

问题


Can someone help me figure out what I'm doing wrong here? IE8 returns 0 for the width of these images regardless. Works fine everywhere else.

$('#hometown li img').load(function()
{
    var imgWidth = parseInt($(this).width());
    var percent = (99.99 * (imgWidth / 1600)) + '%';

    console.log(imgWidth) // <- Always 0 in ie8

    $(this).parent().css({ 'width': percent });
});

回答1:


Try attaching a native onload to each image seperately instead, and trigger the onload if the complete property is true to avoid caching issues :

$('#hometown li img').each(function() {
    this.onload = function() {
        var imgWidth = this.width();
        var percent = 99.99 * (imgWidth / 1600);

        console.log(imgWidth);

        this.parentNode.style.width = percent + '%';
    }
    if(this.complete) this.onload();
});


来源:https://stackoverflow.com/questions/17733343/image-width-after-load-returns-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!