Skip to content
New issue

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

如何实现图片懒加载? #12

Open
suukii opened this issue Jul 2, 2020 · 0 comments
Open

如何实现图片懒加载? #12

suukii opened this issue Jul 2, 2020 · 0 comments

Comments

@suukii
Copy link
Owner

suukii commented Jul 2, 2020

方案一:clientHeight + scrollTop + offsetTop

先给图片一个占位资源,然后通过监听 scroll 事件来判断图片是否到达视口,再给图片设置真实的 src 属性。最好对 scroll 事件做节流处理,以免频繁触发。

img-lazyload

方案二:getBoundingClientRect

在方案一的基础上换了一种方式来判断图片是否到达视口,使用 imgEle.getBoundingClientRect.top() < clientHeight 来判断。

方案三:IntersectionObserver

这个浏览器 API 实现了监听window的scroll事件判断观察对象是否在视口中 以及 节流 三大功能。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant