加载后的图像宽度返回0

Image width after load returns 0

本文关键字:返回 图像 加载      更新时间:2023-09-26

有人能帮我找出我做错了什么吗?不管这些图像的宽度如何,IE8都会返回0。其他地方都很好。

$('#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 });
});

尝试将本机加载单独附加到每个映像,如果完整属性为true,则触发加载以避免缓存问题:

$('#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();
});