pathii's picture
morden blog site using HTML and tailwind css
355877e verified
document.addEventListener('DOMContentLoaded', () => {
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
if (targetId === '#') return;
const targetElement = document.querySelector(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth'
});
}
});
});
// Lazy loading for images
if ('loading' in HTMLImageElement.prototype) {
const lazyImages = document.querySelectorAll('img[loading="lazy"]');
lazyImages.forEach(img => {
img.src = img.dataset.src;
});
} else {
// Fallback for browsers that don't support native lazy loading
const lazyLoad = () => {
const lazyImages = document.querySelectorAll('img[data-src]');
const lazyImageObserver = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
lazyImageObserver.unobserve(lazyImage);
}
});
});
lazyImages.forEach((lazyImage) => {
lazyImageObserver.observe(lazyImage);
});
};
document.addEventListener('DOMContentLoaded', lazyLoad);
window.addEventListener('load', lazyLoad);
window.addEventListener('resize', lazyLoad);
window.addEventListener('scroll', lazyLoad);
}
// Feather icons replacement
if (typeof feather !== 'undefined') {
feather.replace();
}
});