We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
参考链接:Intersection Observer API - Web API | MDN
LazyLoadImage
data-src
src
// 引入polyfill,解决兼容性问题 import 'intersection-observer'; IntersectionObserver.prototype['THROTTLE_TIMEOUT'] = 300; const DefaultLoadingImage = '默认loading图片' export default class LazyLoadImage { _observer: IntersectionObserver | null; _loadingImage: string; constructor(option: object = {}) { this._observer = null; // @ts-ignore this._loadingImage = option.loading || DefaultLoadingImage; this.init(); } init() { this._observer = new IntersectionObserver(entries => { entries.forEach(entry => { // 触发进入视窗条件,替换真正的图片链接到src属性上 if (entry.isIntersecting) { // @ts-ignore const url = entry.target.getAttribute('data-src'); // @ts-ignore entry.target.setAttribute('src', url); entry.target.setAttribute('data-src', ''); // 替换真正的线上地址后,取消对该元素的观察 this._observer && this._observer.unobserve(entry.target); } }) }, { root: null, rootMargin: "500px 200px 1000px 200px", // 扩大视窗范围,提前加载 threshold: 0.1 }) } // 让每个img标签自行调用add方法,把自己添加到观察者队列中 add(entry: any) { this._observer && this._observer.observe(entry.el); } }
// 全局只实例化一个类,实例执行init方法自己创建观察者队列 const lazyload = new LazyLoadImage(); // 让每个img标签自行调用add方法,把自己添加到观察者队列中 // 用法: <img v-lazy="图片地址" /> Vue.directive('lazy', { bind(el, binding) { el.setAttribute('data-src', binding.value); el.setAttribute('src', lazyload._loadingImage); lazyload.add({el: el, val: binding.value}); } })
The text was updated successfully, but these errors were encountered:
No branches or pull requests
基础(必会)
参考链接:Intersection Observer API - Web API | MDN
思路
LazyLoadImage
类,全局只实例化一次,执行init方法data-src
属性中data-src
属性中取出真正的地址,赋予给src
属性,完成加载创建一个全局的
LazyLoadImage
类封装vue指令
The text was updated successfully, but these errors were encountered: