$(function() { if( $('.index_01 li').length > 1 ){ $('.index_01 ul').addClass('swiper-wrapper'); $('.index_01 li').addClass('swiper-slide'); var index_01a = new Swiper('.index_01', { autoplay: { delay: 5000, disableOnInteraction: false }, loop: true, spaceBetween: 0, pagination: { el: '.swiper-pagination', clickable: true, }, navigation: { nextEl: '.next', prevEl: '.prev', }, }); } function initAdvancedTiltEffect() { if( $('.index_01_nomove').length ) return; var container = $('.index_01'); var image = container.find('img'); var maxTilt = 8; var maxMovement = 15; gsap.set(container, { transformPerspective: 1000, transformStyle: "preserve-3d", position: "relative", overflow: "hidden" }); // gsap.set(image, { // position: "relative", // transform: "translateZ(0)" // }); container.on('mousemove', function(e) { var rect = this.getBoundingClientRect(); var x = (e.clientX - rect.left) / rect.width; var y = (e.clientY - rect.top) / rect.height; var tiltX = (y - 0.5) * maxTilt * -1; var tiltY = (x - 0.5) * maxTilt; var moveX = (x - 0.5) * maxMovement * -1; var moveY = (y - 0.5) * maxMovement * -1; gsap.to(container, { duration: 0.4, rotationX: tiltX, rotationY: tiltY, ease: "power2.out" }); // gsap.to(image, { // duration: 0.4, // x: moveX, // y: moveY, // ease: "power2.out" // }); }); // 鼠标离开时恢复原状 container.on('mouseleave', function() { gsap.to(container, { duration: 1, rotationX: 0, rotationY: 0, ease: "elastic.out(1, 0.5)" }); // gsap.to(image, { // duration: 1, // x: 0, // y: 0, // ease: "elastic.out(1, 0.5)" // }); }); } // container.on('mouseenter', function() { // container.addClass('tilt-active'); // }); // // 鼠标离开时移除光影效果 // container.on('mouseleave', function() { // container.removeClass('tilt-active'); // }); // 调用初始化函数 initAdvancedTiltEffect(); // 窗口大小改变时重新初始化 $(window).on('resize', debounce(function() { initAdvancedTiltEffect(); }, 250)); // 防抖函数 function debounce(func, wait) { let timeout; return function() { clearTimeout(timeout); timeout = setTimeout(function() { func.apply(this, arguments); }, wait); }; } });