diff --git a/assets/style.css b/assets/style.css index edc2d5fd..6f750cda 100644 --- a/assets/style.css +++ b/assets/style.css @@ -554,6 +554,14 @@ nav{ color: #1d0f44ad; transition: all 0.3s ease; } + +.lazy { + opacity: 0; + transition: opacity 0.3s; +} +.loaded { + opacity: 1; +} /* Footer Section Ends */ /* CSS for Light-Dark Theme SWITCH */ .theme-toggle { @@ -622,4 +630,4 @@ a { bottom: 0; width: 100%; max-width: 700px; - } \ No newline at end of file + } diff --git a/index.html b/index.html index ecd6b124..61c31b5e 100644 --- a/index.html +++ b/index.html @@ -2,6 +2,10 @@ +
+ + + @@ -93,6 +97,7 @@

About Us

+
Projects
Projects
diff --git a/script.js b/script.js index 69808622..6752f011 100644 --- a/script.js +++ b/script.js @@ -204,6 +204,50 @@ function typeLetter() { const intervalId = setInterval(typeLetter, 100); +(function() { + 'use strict'; + + var lazyElements = [].slice.call(document.querySelectorAll('.lazy')); + + var loadLazyContent = function(lazyElement) { + var url = lazyElement.getAttribute('data-src'); + + // Make an AJAX request to fetch the lazy content + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.status === 200) { + // Set the fetched content as the innerHTML of the lazy element + lazyElement.innerHTML = xhr.responseText; + lazyElement.classList.add('loaded'); + } + } + }; + xhr.open('GET', url, true); + xhr.send(); + }; + + var lazyLoadHandler = function() { + lazyElements.forEach(function(lazyElement) { + var rect = lazyElement.getBoundingClientRect(); + + // Check if the lazy element is within the viewport + if (rect.top >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)) { + loadLazyContent(lazyElement); + } + }); + + // Remove the event listener once all lazy elements are loaded + if (lazyElements.length === 0) { + window.removeEventListener('scroll', lazyLoadHandler); + } + }; + + // Add the lazyLoadHandler to the scroll event + window.addEventListener('scroll', lazyLoadHandler); + lazyLoadHandler(); // Call it once to load any initially visible elements + })(); + function toggleTheme() { var slider = document.getElementById("themeToggle"); if (slider.checked) {