(Example in CodePen: https://codepen.io/rasaf-ibrahim/pen/mdrVQEq )
We need to add extra classes on HTML and change classes on CSS and JS to make multiple swiper slideshow work in a single page.
Step-1:
Let’s first take a look at the HTML files of the swiper slideshow
<!— HTML of swiper-1 -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?car)"></div>
<div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?tree)"></div>
<div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?tiger)"></div>
<div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?water)"></div>
<div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?dog)"></div>
</div>
<!-- Swiper - Add Pagination -->
<div class="swiper-pagination"></div>
</div>
<!—HTML of swiper-2 -->
<div class="swiper-container ">
<div class="parallax-bg" style="background-image:url(https://source.unsplash.com/800x600/?space)" data-swiper-parallax="-23%"></div>
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="title" data-swiper-parallax="-300">Slide 1</div>
<div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
<div class="text" data-swiper-parallax="-100">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo. </p>
</div>
</div>
<div class="swiper-slide">
<div class="title" data-swiper-parallax="-300" data-swiper-parallax-opacity="0">Slide 2</div>
<div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
<div class="text" data-swiper-parallax="-100">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo. </p>
</div>
</div>
<div class="swiper-slide">
<div class="title" data-swiper-parallax="-300">Slide 3</div>
<div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
<div class="text" data-swiper-parallax="-100">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo. Integer laoreet
</p>
</div>
</div>
</div>
<!-- Add Pagination -->
<div class="swiper-pagination swiper-pagination-white"></div>
<!-- Add Navigation -->
<div class="swiper-button-prev swiper-button-white"></div>
<div class="swiper-button-next swiper-button-white"></div>
</div>
Step -2:
Let’s first Identify classes that are common in the CSS file of both swiper slideshow.
/* CSS of swiper-1 */
.swiper-container {
width: 100%;
padding-top: 50px;
padding-bottom: 50px;
}
.swiper-slide {
background-position: center;
background-size: cover;
width: 300px;
height: 300px;
}
/* CSS of swiper-2 */
.swiper-container {
position: relative;
height: 100%;
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: #000;
}
.swiper-slide {
font-size: 18px;
color: white;
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 40px 60px;
}
.parallax-bg {
position: absolute;
left: 0;
top: 0;
width: 130%;
height: 100%;
-webkit-background-size: cover;
background-size: cover;
background-position: center;
}
.swiper-slide .title {
font-size: 41px;
font-weight: 300;
}
.swiper-slide .subtitle {
font-size: 21px;
}
.swiper-slide .text {
font-size: 14px;
max-width: 400px;
line-height: 1.3;
}
As we can see,
.swiper-container and .swiper-slide are common classes
Step -3:
Now, let’s identify classes that are common in the JS file of both swiper slideshow.
// JS File of Swiper-1
var swiper = new Swiper('.swiper-container ', {
effect: 'coverflow',
grabCursor: true,
centeredSlides: true,
slidesPerView: 'auto',
coverflowEffect: {
rotate: 50,
stretch: 0,
depth: 100,
modifier: 1,
slideShadows: true,
},
pagination: {
el: '.swiper-pagination',
},
});
// JS file of swiper-2
var swiper = new Swiper('.swiper-container', {
speed: 600,
parallax: true,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
As we can see,
.swiper-container and .swiper-pagination are common classes
Step-4:
Now let’s add extra classes on the common classes (.swiper-container .swiper-slide .swiper-pagination) of the HTML file
<!— HTML of swiper-1 -->
<div class="swiper-container swiper-container1">
<div class="swiper-wrapper">
<div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?car)"></div>
<div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?tree)"></div>
<div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?tiger)"></div>
<div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?water)"></div>
<div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?dog)"></div>
</div>
<!-- Swiper - Add Pagination -->
<div class="swiper-pagination swiper-pagination1"></div>
</div>
<!—HTML of swiper-2 -->
<div class="swiper-container swiper-container2">
<div class="parallax-bg" style="background-image:url(https://source.unsplash.com/800x600/?space)" data-swiper-parallax="-23%"></div>
<div class="swiper-wrapper">
<div class="swiper-slide swiper-slide2">
<div class="title" data-swiper-parallax="-300">Slide 1</div>
<div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
<div class="text" data-swiper-parallax="-100">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo.</p>
</div>
</div>
<div class="swiper-slide swiper-slide2">
<div class="title" data-swiper-parallax="-300" data-swiper-parallax-opacity="0">Slide 2</div>
<div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
<div class="text" data-swiper-parallax="-100">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo. </p>
</div>
</div>
<div class="swiper-slide swiper-slide2">
<div class="title" data-swiper-parallax="-300">Slide 3</div>
<div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
<div class="text" data-swiper-parallax="-100">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo.</p>
</div>
</div>
</div>
<!-- Add Pagination -->
<div class="swiper-pagination swiper-pagination2 swiper-pagination-white"></div>
<!-- Add Navigation -->
<div class="swiper-button-prev swiper-button-white"></div>
<div class="swiper-button-next swiper-button-white"></div>
</div>
Step-5:
Now let’s change those common classes with the newly added classes on both CSS and JS.
CSS:
/* CSS of swiper-1 */
.swiper-container1{
width: 100%;
padding-top: 50px;
padding-bottom: 50px;
}
.swiper-slide1 {
background-position: center;
background-size: cover;
width: 300px;
height: 300px;
}
/* CSS of swiper-2 */
.swiper-container2{
position: relative;
height: 100%;
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: #000;
}
.swiper-slide2 {
font-size: 18px;
color: white;
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 40px 60px;
}
.parallax-bg {
position: absolute;
left: 0;
top: 0;
width: 130%;
height: 100%;
-webkit-background-size: cover;
background-size: cover;
background-position: center;
}
.swiper-slide .title {
font-size: 41px;
font-weight: 300;
}
.swiper-slide .subtitle {
font-size: 21px;
}
.swiper-slide .text {
font-size: 14px;
max-width: 400px;
line-height: 1.3;
}
JS:
// JS of swiper-1
var swiper = new Swiper('.swiper-container1', {
effect: 'coverflow',
grabCursor: true,
centeredSlides: true,
slidesPerView: 'auto',
coverflowEffect: {
rotate: 50,
stretch: 0,
depth: 100,
modifier: 1,
slideShadows: true,
},
pagination: {
el: '.swiper-pagination1',
},
});
// JS of swiper-2
var swiper = new Swiper('.swiper-container2', {
speed: 600,
parallax: true,
pagination: {
el: '.swiper-pagination2',
clickable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});