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

Lazy loading is added #450

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -622,4 +630,4 @@ a {
bottom: 0;
width: 100%;
max-width: 700px;
}
}
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<html lang="en">

<head>
<div class="lazy" data-src="index.html"></div>

<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script>

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
Expand Down Expand Up @@ -93,6 +97,7 @@ <h3>About Us</h3>
</div>
</div>
</section>
<div class="title1">Projects</div>

<div class="title1" id="project">Projects</div>

Expand Down
44 changes: 44 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down