js - 判断图片是否加载完成

做聊天室的时候,如果又图片并不会滚动到底部。这时候图片还没加载完成,需要判断聊天图片是否加载完成

var imgAllLoad = (cb) => {
    var flag = 0
    var all = [...document.querySelectorAll('img')]
    //  .filter(e => e.className == 'img-style')
    all.forEach((elm, index, arr) => {
        let handle = () => {
            if(flag == arr.length){
                cb()
            }
        }
        if(elm.complete){
            flag++  
            handle()
            return
        }
        elm.onload = () => {
            flag++  
            handle()
        }
    })
}
  • VUE例子
scrollChatMessageBotton(){
            this.$nextTick(() => {
                try {
                    this.imgAllLoad(() => {
                        this.$refs.chat_list[this.chatList.length - 1].scrollIntoView({
                            behavior: 'smooth',
                            block: 'end',
                            inline: 'end'
                        })   
                    })
                } catch (error) {
                    console.log('scrollChatMessageBotton')
                    console.log(error)
                }
            })
        }

上面的filter可以只限制到聊天室的图片上面

--END--

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容