I have a swiper positioned inside a tab on a wordpress site, thus it is not initiated on page load, so the navigation function does not work. You can see it here by clicking on the “görüşler” tab.
I did a research and I found out that it’s a common issue and it can be initiated with some custom jquery. I’ve added the following code as an external javascript, however I can’t get it to work. If anyone can help me out, I’d appreciate it as it is driving me insane. Many thanks in advance.
JavaScript
x
22
22
1
jQuery(document).on("pageinit", function($) {
2
var swiper = new Swiper('.swiper-container', {
3
parallax: true,
4
autoHeight: true,
5
setWrapperSize: true,
6
slidesPerView: 1,
7
spaceBetween: 30,
8
centeredSlides: true,
9
autoplay: {
10
delay: 2500,
11
disableOnInteraction: false,
12
},
13
navigation: {
14
nextEl: '.swiper-button-next',
15
prevEl: '.swiper-button-prev',
16
},
17
observer: true,
18
observeParents: true,
19
});
20
swiper.init();
21
});
22
Advertisement
Answer
I ended up using this code, posting it here in case anyone needs it.
JavaScript
1
16
16
1
// Call On Page Load
2
3
var observer = new IntersectionObserver( (entries, observer) => {
4
entries.forEach(entry => {
5
if (entry.intersectionRatio > 0) {
6
window.dispatchEvent(new Event('resize'));
7
observer.unobserve(entry.target);
8
if (entry.target.swiper) {
9
entry.target.swiper.slideTo(0);
10
}
11
};
12
});
13
}, null);
14
15
observer.observe(document.querySelector('#yepss .swiper-container'));
16