From fcad8422229db7f462b6499609b95c53aec71e1e Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 20 Mar 2023 14:30:04 +0100 Subject: [PATCH] early loading if visible --- last-icon.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/last-icon.js b/last-icon.js index 8b1a360..b8f8617 100644 --- a/last-icon.js +++ b/last-icon.js @@ -286,6 +286,20 @@ function refreshIcon(inst, iconName, iconSet, iconType) { }); } +/** + * @param {HTMLElement} element + * @returns {Boolean} + */ +function isInViewport(element) { + const rect = element.getBoundingClientRect(); + return ( + rect.top >= 0 && + rect.left >= 0 && + rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && + rect.right <= (window.innerWidth || document.documentElement.clientWidth) + ); +} + class LastIcon extends HTMLElement { /** * @param {object} opts @@ -330,10 +344,9 @@ class LastIcon extends HTMLElement { connectedCallback() { // innerHTML is not available because not parsed yet + // setTimeout also allows whenDefined to kick in before init setTimeout(() => { - // Do this is setTimeout to allow whenDefined to kick in before - // otherwise it cannot be configured - if (options.lazy) { + if (options.lazy && !isInViewport(this)) { // observer will call init when element is visible observer.observe(this); } else {