Untitled
unknown
plain_text
2 years ago
6.5 kB
10
Indexable
'use strict';
const nav = document.querySelector('.nav');
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 header = document.querySelector('.header');
///////////////////////////////////////
// Modal window
const openModal = function (e) {
e.preventDefault(); // to prevent the website from going back to the top when opening the modal
modal.classList.remove('hidden');
overlay.classList.remove('hidden');
e.stopPropagation();
};
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
document.querySelector('.nav__links').addEventListener('click', function (e) {
e.preventDefault();
// console.log(e.target);
// Matching strategy
if (e.target.classList.contains('nav__link')) {
const id = e.target.getAttribute('href');
console.log(id);
document.querySelector(id).scrollIntoView({ behavior: 'smooth' });
}
});
///////////////////////////////////////
// Tabbed component
tabsContainer.addEventListener('click', function (e) {
const clicked = e.target.closest('.operations__tab');
// console.log(clicked);
// Guard clause
if (!clicked) return;
// remove all active classes from tab and content area
tabs.forEach((tab) => tab.classList.remove('operations__tab--active'));
tabsContent.forEach((tabContent) =>
tabContent.classList.remove('operations__content--active')
);
// activate tab and content area
clicked.classList.add('operations__tab--active');
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 an argument into handler
// * mouseenter/mouseleave does not bubble
nav.addEventListener('mouseover', handleHover.bind(0.5));
nav.addEventListener('mouseout', handleHover.bind(1));
///////////////////////////////////////
// Sticky navigation - scroll event
const navHeight = nav.getBoundingClientRect().height;
// console.log(navHeight);
const stickyNav = function (entries) {
const [entry] = entries;
if (!entry.isIntersecting) nav.classList.add('sticky');
else nav.classList.remove('sticky');
};
const headerObserver = new IntersectionObserver(stickyNav, {
root: null,
threshold: 0, // 0 percent of the header is visible, we want something to happen
rootMargin: `-${navHeight}px`, // px height of nav
});
headerObserver.observe(header);
///////////////////////////////////////
// Reveal elements
const allSections = document.querySelectorAll('.section');
const revealSection = function (entries, observer) {
const [entry] = entries;
// console.log(entry);
if (!entry.isIntersecting) return;
entry.target.classList.remove('section--hidden');
observer.unobserve(entry.target);
};
const sectionObserver = new IntersectionObserver(revealSection, {
root: null,
threshold: 0.15,
});
allSections.forEach((section) => {
sectionObserver.observe(section);
section.classList.add('section--hidden');
});
///////////////////////////////////////
// Lazy loading images
const imgTargets = document.querySelectorAll('img[data-src]');
const loadImg = function (entries, observer) {
const [entry] = entries;
// console.log(entry);
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: '200px',
});
imgTargets.forEach((img) => {
// imgObserver.observe(img);
});
///////////////////////////////////////
// Slider part 1
const slides = document.querySelectorAll('.slide');
const slider = document.querySelector('.slider');
const btnLeft = document.querySelector('.slider__btn--left');
const btnRight = document.querySelector('.slider__btn--right');
// slider.style.transform = 'scale(0.4) translateX(-800px)';
// slider.style.overflow = 'visible';
// slides.forEach((slide, i) => {
// slide.style.transform = `translateX(${i * 100}%)`; // 0%, 100%, 200%, 300%
// });
const goToSlide = function (slide) {
slides.forEach((s, i) => {
s.style.transform = `translateX(${(i - slide) * 100}%)`; // slide 1: -100%, 0%, 100%, 200%
});
};
goToSlide(0);
let curSlide = 0;
const maxSlide = slides.length;
// Next slide and prev slide
const nextSlide = function () {
if (curSlide === maxSlide - 1) curSlide = 0;
else curSlide++;
goToSlide(curSlide);
};
const prevSlide = function () {
if (curSlide === 0) curSlide = maxSlide - 1;
else curSlide--;
goToSlide(curSlide);
};
btnRight.addEventListener('click', nextSlide);
btnLeft.addEventListener('click', prevSlide);
Editor is loading...
Leave a Comment