A client-side hook that initializes and manages Swiper slider instances with fade effects, autoplay, and intersection observer optimization.
import SwiperSliderInit from '@/hooks/swiper-slider-init'export default function MySlider() {return (<><divid="my-slider"className="swiper"><div className="swiper-wrapper"><div>Slide 1</div><div>Slide 2</div><div>Slide 3</div></div><div className="swiper-pagination"></div></div><SwiperSliderInit swiperId="my-slider" /></>)}
swiperId (string, required): The ID of the HTML element that contains the Swiper sliderThe Swiper is configured with:
The component is used in src/components/swiper.tsx to initialize slider functionality.
Autoplay is initialized as false and only enabled when the swiper enters the viewport. This is intentional and should not be changed.
Problem: When autoplay runs on a swiper that is not in view, the slide transitions can cause height changes (especially with autoHeight: true). These height changes trigger browser scroll events, which the useOffsetTop hook interprets as user scrolling. This caused the sticky navbar to appear unexpectedly—even when the user wasn't scrolling.
Solution: By disabling autoplay until the swiper is actually visible:
See the inView effect that configures and starts autoplay only when the swiper enters the viewport.