julienmonnerie/assets/js/app.js

101 lines
2.5 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 sliderSection = document.querySelector('.slider-section');
let slider;
const slides = document.querySelectorAll('.splide__slide');
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
}
}
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();
mountSlider();
changeSlideOnClick();
changeSlideOnSwipe();