Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 17:34, 7 February 2025 by Creatorless (talk | contribs) (Created page with "document.addEventListener('DOMContentLoaded', function () { // === 1. Sticky Header on Scroll === const header = document.querySelector('header'); let lastScrollTop = 0; window.addEventListener('scroll', function () { let currentScroll = window.pageYOffset || document.documentElement.scrollTop; if (currentScroll > lastScrollTop) { header.classList.add('sticky'); } else { header.classList.remove('sticky');...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
document.addEventListener('DOMContentLoaded', function () {

    // === 1. Sticky Header on Scroll ===
    const header = document.querySelector('header');
    let lastScrollTop = 0;
    window.addEventListener('scroll', function () {
        let currentScroll = window.pageYOffset || document.documentElement.scrollTop;
        if (currentScroll > lastScrollTop) {
            header.classList.add('sticky');
        } else {
            header.classList.remove('sticky');
        }
        lastScrollTop = currentScroll <= 0 ? 0 : currentScroll;
    });

    // === 2. Dynamic Content Loading (AJAX) ===
    const loadMoreButton = document.getElementById('load-more');
    loadMoreButton.addEventListener('click', function () {
        loadMoreContent();
    });

    function loadMoreContent() {
        const contentContainer = document.getElementById('dynamic-content');
        const loader = document.createElement('div');
        loader.classList.add('loader');
        contentContainer.appendChild(loader);

        setTimeout(() => {
            const newContent = document.createElement('div');
            newContent.classList.add('new-content');
            newContent.innerHTML = `<h2>New Dynamic Content</h2><p>This content was dynamically loaded via AJAX.</p>`;
            contentContainer.removeChild(loader);
            contentContainer.appendChild(newContent);
        }, 1500); // Simulating an AJAX request delay
    }

    // === 3. Smooth Scroll for Anchors ===
    const scrollLinks = document.querySelectorAll('a[href^="#"]');
    scrollLinks.forEach(link => {
        link.addEventListener('click', function (event) {
            event.preventDefault();
            const target = document.querySelector(this.getAttribute('href'));
            window.scrollTo({
                top: target.offsetTop - 100, // Offset for sticky navigation
                behavior: 'smooth'
            });
        });
    });

    // === 4. Custom Theme Toggle (Light/Dark Mode) ===
    const themeToggleButton = document.getElementById('theme-toggle');
    themeToggleButton.addEventListener('click', function () {
        document.body.classList.toggle('dark-mode');
        localStorage.setItem('theme', document.body.classList.contains('dark-mode') ? 'dark' : 'light');
    });

    // === 5. Load Theme Preference from Local Storage ===
    if (localStorage.getItem('theme') === 'dark') {
        document.body.classList.add('dark-mode');
    }

    // === 6. Tooltip Implementation for Links ===
    const tooltipLinks = document.querySelectorAll('[data-tooltip]');
    tooltipLinks.forEach(link => {
        link.addEventListener('mouseenter', function () {
            const tooltipText = this.getAttribute('data-tooltip');
            const tooltip = document.createElement('div');
            tooltip.classList.add('tooltip');
            tooltip.innerText = tooltipText;
            document.body.appendChild(tooltip);

            const rect = this.getBoundingClientRect();
            tooltip.style.top = rect.top - tooltip.offsetHeight - 10 + 'px';
            tooltip.style.left = rect.left + (rect.width / 2) - (tooltip.offsetWidth / 2) + 'px';
        });

        link.addEventListener('mouseleave', function () {
            const tooltip = document.querySelector('.tooltip');
            if (tooltip) {
                tooltip.remove();
            }
        });
    });

    // === 7. Button Hover Animations ===
    const hoverButtons = document.querySelectorAll('.hover-button');
    hoverButtons.forEach(button => {
        button.addEventListener('mouseenter', function () {
            this.classList.add('hover');
        });

        button.addEventListener('mouseleave', function () {
            this.classList.remove('hover');
        });
    });

    // === 8. Add Fade-In Animation on Page Load ===
    document.body.classList.add('fade-in');

});

// === 9. CSS for Animations & Features (Use this in your CSS file) ===

/* Sticky Header */
header.sticky {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background-color: rgba(20, 10, 40, 0.8);
    z-index: 1000;
}

/* Dark Mode */
body.dark-mode {
    background-color: #121212;
    color: #ffffff;
}

body.dark-mode header {
    background-color: rgba(20, 10, 40, 1);
}

/* Tooltip */
.tooltip {
    position: absolute;
    background-color: rgba(0, 0, 0, 0.7);
    color: #fff;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 12px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    z-index: 9999;
}

/* Hover Effect for Buttons */
.hover-button {
    position: relative;
    overflow: hidden;
    transition: all 0.3s ease;
}
.hover-button.hover {
    transform: scale(1.1);
    box-shadow: 0 0 15px rgba(255, 0, 255, 0.5);
}

/* Fade-In Animation on Page Load */
body.fade-in {
    opacity: 0;
    animation: fadeIn 1s forwards;
}

@keyframes fadeIn {
    to {
        opacity: 1;
    }
}

/* Loader */
.loader {
    width: 30px;
    height: 30px;
    border: 4px solid rgba(255, 0, 255, 0.3);
    border-top: 4px solid #ff00ff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Smooth Scroll */
html {
    scroll-behavior: smooth;
}

/* Button Hover */
button {
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}
button:hover {
    transform: scale(1.1);
    box-shadow: 0 0 15px rgba(255, 0, 255, 0.7);
}