'use strict'; const modal = document.querySelector('.modal'); const overlay = document.querySelector('.overlay'); const btnCloseModal = document.querySelector('.btn--close-modal'); const btnsOpenModal = document.querySelectorAll('.btn--show-modal'); const btnScrollTo = document.querySelector('.btn--scroll-to'); const section1 = document.querySelector('#section--1'); const tabs = document.querySelectorAll('.operations__tab'); const tabsContainer = document.querySelector('.operations__tab-container'); const tabsContent = document.querySelectorAll('.operations__content'); const nav = document.querySelector('.nav'); const header = document.querySelector('.header'); alert( "This page is not responsive. I've mainly focused on javascript. Please view in larger devices!" ); /////////////////////////////////////// // Modal window const openModal = function (e) { e.preventDefault; modal.classList.remove('hidden'); overlay.classList.remove('hidden'); }; const closeModal = function () { modal.classList.add('hidden'); overlay.classList.add('hidden'); }; btnsOpenModal.forEach(btn => btn.addEventListener('click', openModal)); btnCloseModal.addEventListener('click', closeModal); overlay.addEventListener('click', closeModal); document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && !modal.classList.contains('hidden')) { closeModal(); } }); /////////////////////////////////////// // Button scrolling btnScrollTo.addEventListener('click', function (e) { section1.scrollIntoView({ behavior: 'smooth' }); }); /////////////////////////////////////// // Page navigation ////Event delegation document.querySelector('.nav__links').addEventListener('click', e => { e.preventDefault(); //Matching if (e.target.classList.contains('nav__link')) { const id = e.target.getAttribute('href'); document.querySelector(id).scrollIntoView({ behavior: 'smooth' }); } }); /////////////////////////////////////// // Tabbed Component //using event delegation tabsContainer.addEventListener('click', e => { const clicked = e.target.closest('.operations__tab'); //Guard clause if (!clicked) return; if (clicked) { //removing active class tabs.forEach(t => t.classList.remove('operations__tab--active')); tabsContent.forEach(c => c.classList.remove('operations__content--active')); //Active tab clicked.classList.add('operations__tab--active'); //Active content document .querySelector(`.operations__content--${clicked.dataset.tab}`) .classList.add('operations__content--active'); } }); /////////////////////////////////////// // Menu fade animation const handleHover = function (e) { if (e.target.classList.contains('nav__link')) { const link = e.target; const siblings = link.closest('.nav').querySelectorAll('.nav__link'); const logo = link.closest('.nav').querySelector('img'); siblings.forEach(el => { if (el !== link) { el.style.opacity = this; logo.style.opacity = this; } }); } }; //Passing argument to handler nav.addEventListener('mouseover', handleHover.bind(0.5)); nav.addEventListener('mouseout', handleHover.bind(1)); /////////////////////////////////////// // Sticky navigation const navHeight = nav.getBoundingClientRect().height; const stickyNav = entries => { const [entry] = entries; if (!entry.isIntersecting) nav.classList.add('sticky', 'fadeAnim'); else nav.classList.remove('sticky', 'fadeAnim'); }; const headerObserver = new IntersectionObserver(stickyNav, { root: null, threshold: 0, rootMargin: `-${navHeight}px`, }); headerObserver.observe(header); /////////////////////////////////////// // REveal elements const allSections = document.querySelectorAll('.section'); const revealSection = function (entries, observer) { const [entry] = entries; if (!entry.isIntersecting) return; entry.target.classList.remove('section--hidden'); observer.unobserve(entry.target); }; const sectionObserver = new IntersectionObserver(revealSection, { root: null, threshold: 0.15, //15% }); allSections.forEach(section => { sectionObserver.observe(section); section.classList.add('section--hidden'); }); /////////////////////////////////////// // Lazy Loading images const imgTargets = document.querySelectorAll('img[data-src]'); const loadImg = (entries, observer) => { const [entry] = entries; if (!entry.isIntersecting) return; //Replace src with data-src entry.target.src = entry.target.dataset.src; entry.target.addEventListener('load', function () { entry.target.classList.remove('lazy-img'); }); observer.unobserve(entry.target); }; const imgObserver = new IntersectionObserver(loadImg, { root: null, threshold: 0, rootMargin: '300px', }); imgTargets.forEach(img => imgObserver.observe(img)); //Slider const slider = function () { const slides = document.querySelectorAll('.slide'); const btnLeft = document.querySelector('.slider__btn--left'); const btnRight = document.querySelector('.slider__btn--right'); const dotContainer = document.querySelector('.dots'); let currentSlide = 0; const maxSlide = slides.length; const createDots = function () { // _ is throwaway variable slides.forEach((_, i) => { dotContainer.insertAdjacentHTML( 'beforeend', `` ); }); }; const activateDot = function (slide) { document .querySelectorAll('.dots__dot') .forEach(dot => dot.classList.remove('dots__dot--active')); document .querySelector(`.dots__dot[data-slide="${slide}"]`) .classList.add('dots__dot--active'); }; const goToSlide = function (slide) { slides.forEach( (s, i) => (s.style.transform = `translateX(${100 * (i - slide)}%)`) ); }; //Next slide const nextSlide = function () { if (currentSlide === maxSlide - 1) { currentSlide = 0; } else { currentSlide++; } goToSlide(currentSlide); activateDot(currentSlide); }; const previousSlide = function () { if (currentSlide === 0) { currentSlide = maxSlide - 1; } else { currentSlide--; } goToSlide(currentSlide); activateDot(currentSlide); }; const init = function () { createDots(); goToSlide(0); activateDot(0); }; init(); //Event handlers btnRight.addEventListener('click', nextSlide); btnLeft.addEventListener('click', previousSlide); document.addEventListener('keydown', function (e) { e.key === 'ArrowLeft' && previousSlide(); e.key === 'ArrowRight' && nextSlide(); }); dotContainer.addEventListener('click', e => { if (e.target.classList.contains('dots__dot')) { const { slide } = e.target.dataset; goToSlide(slide); activateDot(slide); } }); }; slider();