julienmonnerie/assets/js/app.js

180 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-08-02 18:50:27 +02:00
import Splide from '@splidejs/splide';
import create from 'swiped-events';
2022-08-02 18:50:27 +02:00
2022-06-17 17:51:59 +02:00
// ----------------------------------------------------------------------------
// DATA
// ----------------------------------------------------------------------------
2022-08-02 18:50:27 +02:00
// UTILS
const body = document.body;
// SLIDER
2022-08-02 18:50:27 +02:00
2022-11-23 14:55:44 +01:00
const homeSection = document.querySelector('.home-section');
const navLinks = document.querySelectorAll('.navigation__link');
const socialLinks = document.querySelectorAll('.social__link');
const sliderSection = document.querySelector('.slider-section');
let slider;
const slides = document.querySelectorAll('.splide__slide');
let cursorOrientation;
let cursorColor;
2022-06-17 17:51:59 +02:00
// ----------------------------------------------------------------------------
2022-07-28 18:59:14 +02:00
// LOGIC
2022-06-17 17:51:59 +02:00
// ----------------------------------------------------------------------------
2022-07-28 18:59:14 +02:00
// UTILS
// Enable CSS :active pseudo-class in Safari Mobile
function enableActivePseudoClass() {
2022-09-09 15:26:04 +02:00
document.addEventListener("touchstart", function() {},false);
2022-07-28 18:59:14 +02:00
}
2022-06-17 17:51:59 +02:00
// Convert rem to pixels by getting font-size CSS property
function convertRemToPixels(rem) {
2022-09-09 15:26:04 +02:00
let fontSize = parseFloat(window.getComputedStyle(body).getPropertyValue('font-size'));
return rem * fontSize;
2022-06-17 17:51:59 +02:00
}
// SLIDER
2022-08-02 18:50:27 +02:00
function setUpSlider() {
2022-11-23 14:55:44 +01:00
if (sliderSection) {
slider = new Splide('.splide', {
2022-09-09 15:26:04 +02:00
type: 'fade',
rewind: true,
rewindByDrag: true,
speed: 400,
width: '100vw',
height: '100vh',
2022-09-09 15:26:04 +02:00
arrows: false,
pagination: false,
easing: 'ease-in-out',
drag: true
2022-09-09 15:26:04 +02:00
});
}
2022-08-02 18:50:27 +02:00
}
function mountSlider() {
2022-11-23 14:55:44 +01:00
if (slider) {
slider.mount();
2022-09-09 15:26:04 +02:00
}
}
function changeSlideOnClick() {
2022-11-23 14:55:44 +01:00
if (homeSection && slider) {
homeSection.addEventListener('click', function(e) {
2022-11-25 14:41:12 +01:00
if (!e.target.closest('.navigation__link') && !e.target.closest('.social__link')) {
if (e.clientX >= window.innerWidth / 2) {
slider.go('>');
} else {
slider.go('<');
}
}
});
2022-09-09 15:26:04 +02:00
}
2022-08-02 18:50:27 +02:00
}
function changeSlideOnSwipe() {
2022-11-23 14:55:44 +01:00
if (homeSection && slider) {
homeSection.addEventListener('swiped', function(e) {
2022-11-25 15:42:36 +01:00
if (e.detail.dir === 'left') {
2022-11-23 14:55:44 +01:00
slider.go('>');
2022-11-25 15:42:36 +01:00
} else if (e.detail.dir === 'right') {
2022-11-23 14:55:44 +01:00
slider.go('<');
}
});
2022-09-09 15:26:04 +02:00
}
}
function turnSidebarLinksToWhite() {
2022-11-23 14:55:44 +01:00
if (navLinks) {
for (let i = 0; i < navLinks.length; i++) {
navLinks[i].classList.add('navigation__link--white');
}
}
2022-11-23 14:55:44 +01:00
if (socialLinks) {
for (let i = 0; i < socialLinks.length; i++) {
socialLinks[i].classList.add('social__link--white');
}
2022-09-09 15:26:04 +02:00
}
2022-08-03 16:49:26 +02:00
}
function turnSidebarLinksToBlack() {
2022-11-23 14:55:44 +01:00
if (navLinks) {
for (let i = 0; i < navLinks.length; i++) {
navLinks[i].classList.remove('navigation__link--white');
}
}
2022-11-23 14:55:44 +01:00
if (socialLinks) {
for (let i = 0; i < socialLinks.length; i++) {
socialLinks[i].classList.remove('social__link--white');
}
}
}
function editColorTheme(slide) {
2022-09-09 15:26:04 +02:00
if (slide) {
if (slide.getAttribute('data-logo-color') === 'white') {
turnSidebarLinksToWhite();
cursorColor = 'light';
2022-09-09 15:26:04 +02:00
} else if (slide.getAttribute('data-logo-color') === 'black') {
turnSidebarLinksToBlack();
cursorColor = 'dark';
}
if (body && cursorOrientation && cursorColor) {
body.style.cursor = `url('/icons/cursor-${cursorOrientation}-${cursorColor}.svg') 0 16, auto`;
2022-09-09 15:26:04 +02:00
}
}
}
function editColorThemeOnSliderMounted() {
2022-11-23 14:55:44 +01:00
if (slider && slides) {
slider.on('mounted', function() {
editColorTheme(slides[0]);
2022-09-09 15:26:04 +02:00
})
}
2022-08-03 16:49:26 +02:00
}
function editColorThemeOnSlideActive() {
2022-11-23 14:55:44 +01:00
if (slider) {
slider.on('active', function(e) {
editColorTheme(e.slide);
});
}
}
function setCursorOnMove() {
if (body) {
body.addEventListener('mousemove', function(e) {
if (e.clientX >= window.innerWidth / 2) {
cursorOrientation = 'next';
} else {
cursorOrientation = 'previous';
}
if (cursorOrientation && cursorColor) {
body.style.cursor = `url('/icons/cursor-${cursorOrientation}-${cursorColor}.svg') 0 16, auto`;
}
2022-09-09 15:26:04 +02:00
});
}
2022-08-03 16:49:26 +02:00
}
2022-06-17 17:51:59 +02:00
// ----------------------------------------------------------------------------
// PROGRAM
// ----------------------------------------------------------------------------
2022-07-28 18:59:14 +02:00
// UTILS
enableActivePseudoClass();
2022-07-22 17:53:26 +02:00
// SLIDER
2022-08-02 18:50:27 +02:00
setUpSlider();
editColorThemeOnSliderMounted();
mountSlider();
changeSlideOnClick();
changeSlideOnSwipe();
editColorThemeOnSlideActive();
setCursorOnMove();