kodepilot/main.js

217 lines
6.6 KiB
JavaScript

function goToLogin() {
window.location.href = "/my/";
}
(function () {
if (window.Slide1rSliderLoaded) {
return;
}
window.Slide1rSliderLoaded = true;
class Slide1rSlider {
constructor() {
setTimeout(() => {
this.initSlider();
}, 100);
}
initSlider() {
try {
this.track = document.getElementById('slide1rTrack');
this.slides = document.querySelectorAll('.slide1');
this.prevBtn = document.getElementById('prevBtn');
this.nextBtn = document.getElementById('nextBtn');
if (!this.track || this.slides.length === 0) {
console.warn("Slider DOM elements not found. Retrying...");
setTimeout(() => this.initSlider(), 200);
return;
}
this.currentIndex = 0;
this.totalSlides = this.slides.length;
this.autoPlayDuration = 5000;
this.init();
} catch (err) {
console.error("Slider initialization failed:", err);
}
}
init() {
try {
this.updateSlider();
this.bindEvents();
this.startAutoPlay();
this.bindKeyboardEvents();
} catch (err) {
console.error("Slider init error:", err);
}
}
updateSlider() {
try {
const translateX = -this.currentIndex * 100;
this.track.style.transform = `translateX(${translateX}%)`;
} catch (err) {
console.error("Error updating slider:", err);
}
}
nextSlide() {
try {
this.currentIndex = (this.currentIndex + 1) % this.totalSlides;
this.updateSlider();
this.restartAutoPlay();
} catch (err) {
console.error("Error going to next slide:", err);
}
}
prevSlide() {
try {
this.currentIndex = this.currentIndex === 0 ? this.totalSlides - 1 : this.currentIndex - 1;
this.updateSlider();
this.restartAutoPlay();
} catch (err) {
console.error("Error going to previous slide:", err);
}
}
goToSlide(index) {
try {
this.currentIndex = index;
this.updateSlider();
this.restartAutoPlay();
} catch (err) {
console.error("Error going to slide:", err);
}
}
startAutoPlay() {
try {
this.stopAutoPlay();
this.autoPlayInterval = setInterval(() => {
this.nextSlide();
}, this.autoPlayDuration);
} catch (err) {
console.error("Error starting autoplay:", err);
}
}
stopAutoPlay() {
if (this.autoPlayInterval) {
clearInterval(this.autoPlayInterval);
this.autoPlayInterval = null;
}
}
restartAutoPlay() {
this.stopAutoPlay();
this.startAutoPlay();
}
bindEvents() {
try {
if (this.nextBtn) {
this.nextBtn.addEventListener('click', () => this.nextSlide());
}
if (this.prevBtn) {
this.prevBtn.addEventListener('click', () => this.prevSlide());
}
const container = document.querySelector('.slide1r-container');
if (container) {
container.addEventListener('mouseenter', () => this.stopAutoPlay());
container.addEventListener('mouseleave', () => this.startAutoPlay());
}
let startX = 0;
let endX = 0;
if (this.track) {
this.track.addEventListener('touchstart', (e) => {
startX = e.touches[0].clientX;
this.stopAutoPlay();
});
this.track.addEventListener('touchmove', (e) => {
endX = e.touches[0].clientX;
});
this.track.addEventListener('touchend', () => {
const deltaX = startX - endX;
if (Math.abs(deltaX) > 50) {
if (deltaX > 0) {
this.nextSlide();
} else {
this.prevSlide();
}
}
this.startAutoPlay();
});
}
} catch (err) {
console.error("Error binding events:", err);
}
}
bindKeyboardEvents() {
const keyHandler = (e) => {
const container = document.querySelector('.slide1r-container');
if (container && this.isElementInViewport(container)) {
if (e.key === 'ArrowLeft') {
this.prevSlide();
} else if (e.key === 'ArrowRight') {
this.nextSlide();
}
}
};
document.addEventListener('keydown', keyHandler);
}
isElementInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
}
new Slide1rSlider();
})();
document.addEventListener("DOMContentLoaded", () => {
const track = document.querySelector(".move-slider-track");
const boxes = Array.from(track.children);
// Clone all slides once
boxes.forEach(box => {
const clone = box.cloneNode(true);
track.appendChild(clone);
});
let position = 0;
const speed = 1; // pixels per frame, adjust for faster/slower scroll
function animate() {
position -= speed;
if (Math.abs(position) >= track.scrollWidth / 2) {
position = 0; // reset after first set slides out
}
track.style.transform = `translateX(${position}px)`;
requestAnimationFrame(animate);
}
animate();
});