自定义指令img-lazy

// 1. v-lazy
const LazyLoad = {
    // install
    install(Vue,options){
        // loading
        let defaultSrc = options.default;
        Vue.directive('lazy',{
            bind(el,binding){
                LazyLoad.init(el,binding.value,defaultSrc);
            },
            inserted(el){
                //
                if('IntersectionObserver' in window){
                    LazyLoad.observe(el);
                }else{
                    LazyLoad.listenerScroll(el);
                }

            },
        })
    },
    //
    init(el,val,def){
        // data-src src
        el.setAttribute('data-src',val);
        // src loading
        el.setAttribute('src',def);
    },
    // IntersectionObserver el
    observe(el){
        let io = new IntersectionObserver(entries => {
            let realSrc = el.dataset.src;
            if(entries[0].isIntersecting){
                if(realSrc){
                    el.src = realSrc;
                    el.removeAttribute('data-src');
                }
            }
        });
        io.observe(el);
    },
    // scroll
    listenerScroll(el){
        let handler = LazyLoad.throttle(LazyLoad.load,300);
        LazyLoad.load(el);
        window.addEventListener('scroll',() => {
            handler(el);
        });
    },
    //
    load(el){
        let windowHeight = document.documentElement.clientHeight
        let elTop = el.getBoundingClientRect().top;
        let elBtm = el.getBoundingClientRect().bottom;
        let realSrc = el.dataset.src;
        if(elTop - windowHeight<0&&elBtm > 0){
            if(realSrc){
                el.src = realSrc;
                el.removeAttribute('data-src');
            }
        }
    },
    //
    throttle(fn,delay){
        let timer;
        let prevTime;
        return function(...args){
            let currTime = Date.now();
            let context = this;
            if(!prevTime) prevTime = currTime;
            clearTimeout(timer);

            if(currTime - prevTime > delay){
                prevTime = currTime;
                fn.apply(context,args);
                clearTimeout(timer);
                return;
            }
            timer = setTimeout(function(){
                prevTime = Date.now();
                timer = null;
                fn.apply(context,args);
            },delay);
        }
    }
}
export default LazyLoad;
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以的。 首先,我们需要知道 `vue-lazyload` 是一个 Vue 插件,它提供了一个指令 `v-lazy`,可以用来实现图片懒加载。它的使用方式如下: ```html <img v-lazy="imgUrl"> ``` 其中,`imgUrl` 是图片的地址。当图片进入可视区域时,`v-lazy` 指令会自动将 `img` 标签的 `src` 属性设置为 `imgUrl`,从而实现图片的加载。 接下来,我们可以封装一个自定义指令,来简化 `v-lazy` 的使用。 ```js import Vue from 'vue' import VueLazyload from 'vue-lazyload' Vue.use(VueLazyload, { loading: require('@/assets/loading.gif') }) Vue.directive('lazy', { bind: (el, binding) => { el.setAttribute('data-src', binding.value) el.setAttribute('src', require('@/assets/loading.gif')) }, inserted: el => { const observer = new IntersectionObserver(entries => { if (entries[0].isIntersecting) { const imgSrc = el.getAttribute('data-src') el.setAttribute('src', imgSrc) observer.disconnect() } }) observer.observe(el) } }) ``` 上面的代码中,我们使用 `Vue.directive` 方法定义了一个名为 `lazy` 的自定义指令。该指令的作用和 `v-lazy` 相同,用于实现图片的懒加载。 在自定义指令的 `bind` 钩子函数中,我们获取了图片的地址,并将其设置为 `data-src` 属性的值。同时,我们将 `src` 属性的值设置为一个占位图,这里使用了一个 loading 图片。 在自定义指令的 `inserted` 钩子函数中,我们使用了 `IntersectionObserver` API 来观察图片是否进入了可视区域。当图片进入可视区域时,我们将 `src` 属性的值设置为 `data-src` 属性的值,从而实现图片的加载。 最后,我们使用 `Vue.use` 方法将 `vue-lazyload` 插件安装到 Vue 中,并设置了一个全局的 loading 图片。 接下来,在 Vue 模板中,就可以使用我们自定义的指令了: ```html <img v-lazy="imgUrl"> ``` 其中,`imgUrl` 是图片的地址。 希望这个例子能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值