Skip to content

Instantly share code, notes, and snippets.

@amowu
Created February 19, 2017 09:20
Show Gist options
  • Select an option

  • Save amowu/24893b7f5aa85df24de97fb725a173f5 to your computer and use it in GitHub Desktop.

Select an option

Save amowu/24893b7f5aa85df24de97fb725a173f5 to your computer and use it in GitHub Desktop.
某些靜態資源(比如圖片),只有用戶向下滾動,它們進入視口時才加載,這樣可以節省帶寬,提高網頁性能。
function query(selector) {
return Array.from(document.querySelectorAll(selector));
}
var observer = new IntersectionObserver(
function(changes) {
changes.forEach(function(change) {
var container = change.target;
var content = container.querySelector('template').content;
container.appendChild(content);
observer.unobserve(container);
});
}
);
query('.lazy-loaded').forEach(function (item) {
observer.observe(item);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment