防抖节流函数优化图片懒加载

图片懒加载

只加载视窗内的图片。需要判断图片当前是否在视窗范围内,判断方法:视窗高度+视窗下滑的高度>图片的offsetTop
ps:关于offsetTop、scrollTop、clientTop等一系列值的理解,这里贴一个网上的图片。
在这里插入图片描述图片来源:https://www.jianshu.com/p/44abcc626318
对于图片来说,offsetTop是指该图片的顶端到上层(body)的顶端的距离。

    // 实现图片懒加载
    var num=document.getElementsByTagName('img').length
    var img=document.getElementsByTagName('img')
    var n=0//存储图片加载到的位置,避免每次都是从头遍历
    var isLoadImg=false//当前窗口区的图片是否加载完成
    var _clientHeight=document.documentElement.clientHeight//可视区高度
    var _scrollTop=document.documentElement.scrollTop//滚动条距离顶部高度,卷曲出去的高度
    // 监听窗口变化,重新计算可视区高度
    function computedClientHeight(){
        _clientHeight=document.documentElement.clientHeight
    }
    // 页面载入完毕,加载可视区内的图片
    lazyload()
    function lazyload(){
        // 滚动条距离顶部高度
        isLoadImg = n>=num //n<num说明还没有加载到
        _scrollTop=document.documentElement.scrollTop||document.body.scrollTop || window.scrollY;
        for(var i=n;i<num;i++){
            if(img[i].offsetTop<_clientHeight + _scrollTop){
                if(img[i].getAttribute('src')==''){
                    img[i].src=img[i].getAttribute('data-src')
                    console.log(img[i].offsetTop)
                    console.log(_clientHeight + _scrollTop)
                }
                n=i+1
            }
                
        }
        
        // console.log(img)
    }
    // 防抖节流函数优化
    function debounce(callback,delay){
        let timer
        return function(arg){
            clearTimeout(timer)
            timer=setTimeout(function(){
                callback(arg)
            },delay)
        }
    }
    // 防抖--优化不断触发的窗口变化
     window.addEventListener('resize',debounce(computedClientHeight,1000))
    // 节流函数
    function thro(func,wait,isLoad){
        return function(){
            // 形成闭包
            if(!isLoad){
                setTimeout(function(){
                    func()
                    isLoad=false
                },wait)
            }
        }
    }
    // 节流--优化性能,一直拉 下拉条,100毫秒内只触发一次懒加载
    window.addEventListener('scroll',thro(lazyload,100,isLoadImg))
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeScript(简称TS)是一种由微软开发的开源编程语言,它是JavaScript的一个超集,可以编译成纯JavaScript代码。在TS中,我们可以使用装饰器和泛型等特性来封装防抖节流函数防抖函数节流函数都是用于控制函数执行频率的方法,常用于优化性能和避免重复触发事件。 防抖函数的作用是在一定时间内,如果事件持续触发,则重新计时,直到事件停止触发后才执行函数。常见的应用场景是输入框搜索联想功能。 节流函数的作用是在一定时间内,无论事件触发多少次,只执行一次函数。常见的应用场景是滚动加载数据。 下面是一个使用TS封装防抖节流函数的示例: ```typescript // 防抖函数 function debounce(func: Function, delay: number): Function { let timer: number | null = null; return function (...args: any[]) { if (timer) { clearTimeout(timer); } timer = setTimeout(() => { func.apply(this, args); }, delay); }; } // 节流函数 function throttle(func: Function, delay: number): Function { let timer: number | null = null; return function (...args: any[]) { if (!timer) { timer = setTimeout(() => { func.apply(this, args); timer = null; }, delay); } }; } ``` 使用示例: ```typescript function search(keyword: string) { // 模拟搜索功能 console.log(`Searching for ${keyword}`); } const debouncedSearch = debounce(search, 300); const throttledSearch = throttle(search, 300); debouncedSearch('apple'); debouncedSearch('banana'); debouncedSearch('cherry'); throttledSearch('apple'); throttledSearch('banana'); throttledSearch('cherry'); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值