图片加载完成后执行相应功能

图片异步加载

1.计时器异步加载(缺点很明显,不知道图片加载时长,受网络影响)

$(function(){
    setTimeout(function(){
        console.log(document.getElementById("image").complete)
        // function
    },1000)
})

2.image标签onload属性(简单)

<img src="./long.png" id="image" onload="handleLoad()">
function handleLoad () {
    console.log("完成")
    console.log(document.getElementById("image").height)
    // function
}

3.使用Promise对象(高级)

$(function(){
    loadImageAsync()
})

function loadImageAsync () {
    return new Promise(function(resolve, reject) {
        let image = document.getElementById("image")

        image.onload = function(){
            console.log("完成")
            console.log(document.getElementById("image").height)
            // function
            resolve(image)
        }

        image.onerror = function() {
            reject(new Error('Could not load image'));
        }
    })
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值