julienmonnerie/assets/js/app.js

153 lines
4 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;
2022-08-03 14:55:32 +02:00
// SPLIDE SLIDER
2022-08-02 18:50:27 +02:00
const slider = document.querySelector('.slider-section');
let splideSlider;
const slides = document.querySelectorAll('.splide__slide');
2022-07-22 17:53:26 +02:00
const sidebar = document.querySelector('.sidebar');
const sidebarNavLinks = document.querySelectorAll('.sidebar__nav-link');
const sidebarSocialLinks = document.querySelectorAll('.sidebar__social-link');
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
}
2022-08-03 14:55:32 +02:00
// SPLIDE SLIDER
2022-08-02 18:50:27 +02:00
function setUpSlider() {
2022-09-09 15:26:04 +02:00
if (slider) {
splideSlider = 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() {
if (splideSlider) {
splideSlider.mount();
2022-09-09 15:26:04 +02:00
}
}
2022-08-02 18:50:27 +02:00
function goToNextSlideOnClick() {
if (sidebar && splideSlider) {
sidebar.addEventListener('click', function(e) {
if (!e.target.matches('a') && !e.target.parentElement.matches('a')) {
splideSlider.go('>');
}
});
2022-09-09 15:26:04 +02:00
}
2022-08-02 18:50:27 +02:00
}
function changeSlideOnSwipe() {
if (sidebar && splideSlider) {
sidebar.addEventListener('swiped', function(e) {
if (e.detail.dir === 'right') {
splideSlider.go('>');
} else if (e.detail.dir === 'left') {
splideSlider.go('<');
}
});
2022-09-09 15:26:04 +02:00
}
}
function turnSidebarLinksToWhite() {
if (sidebarNavLinks) {
for (let i = 0; i < sidebarNavLinks.length; i++) {
sidebarNavLinks[i].classList.add('sidebar__nav-link--white');
}
}
if (sidebarSocialLinks) {
for (let i = 0; i < sidebarSocialLinks.length; i++) {
sidebarSocialLinks[i].classList.add('sidebar__social-link--white');
}
2022-09-09 15:26:04 +02:00
}
2022-08-03 16:49:26 +02:00
}
function turnSidebarLinksToBlack() {
if (sidebarNavLinks) {
for (let i = 0; i < sidebarNavLinks.length; i++) {
sidebarNavLinks[i].classList.remove('sidebar__nav-link--white');
}
}
if (sidebarSocialLinks) {
for (let i = 0; i < sidebarSocialLinks.length; i++) {
sidebarSocialLinks[i].classList.remove('sidebar__social-link--white');
}
}
}
function editNavLinksColor(slide) {
2022-09-09 15:26:04 +02:00
if (slide) {
if (slide.getAttribute('data-logo-color') === 'white') {
turnSidebarLinksToWhite();
2022-09-09 15:26:04 +02:00
} else if (slide.getAttribute('data-logo-color') === 'black') {
turnSidebarLinksToBlack();
2022-09-09 15:26:04 +02:00
}
}
}
function editNavLinksColorOnSliderMounted() {
if (splideSlider && slides) {
splideSlider.on('mounted', function() {
editNavLinksColor(slides[0]);
2022-09-09 15:26:04 +02:00
})
}
2022-08-03 16:49:26 +02:00
}
function editNavLinksColorOnSlideActive() {
if (splideSlider) {
splideSlider.on('active', function(e) {
editNavLinksColor(e.slide);
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
2022-08-03 14:55:32 +02:00
// SPLIDE SLIDER
2022-08-02 18:50:27 +02:00
setUpSlider();
editNavLinksColorOnSliderMounted();
mountSlider();
2022-08-02 18:50:27 +02:00
goToNextSlideOnClick();
changeSlideOnSwipe();
editNavLinksColorOnSlideActive();