document.addEventListener("DOMContentLoaded", () => {
const getMobileOperatingSystem = function () {
const n = navigator.userAgent || navigator.vendor || window.opera;
const isMobile = /windows phone/i.test(n) ? "Windows Phone" : /android/i.test(n) ? "Android" : !!/iPad|iPhone|iPod/.test(n) && "iOS"
return isMobile;
};
let rellaxInstance;
const initRellax = () => {
const isMobile = getMobileOperatingSystem();
const isSmallScreen = window.innerWidth < 767;
// Удаляем Rellax, если мобильное устройство или ширина окна меньше 767px
if ((isMobile || isSmallScreen) && rellaxInstance) {
rellaxInstance.destroy();
rellaxInstance = null;
}
// Инициализируем Rellax, если не мобильное устройство и ширина больше 767px
else if (!isMobile && !isSmallScreen && !rellaxInstance) {
rellaxInstance = new Rellax('.rellax', {
center: true
});
}
};
// Троттлинг функции
const throttle = (func, limit) => {
let inThrottle;
return function() {
const args = arguments;
const context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
};
// Инициализация на загрузке
initRellax();
// Добавление обработчика изменения размера окна с троттлингом
window.addEventListener('resize', throttle(initRellax, 200));
});