Customizing SwiperJS to display portion of slides on both ends

I need some assistance with my SwiperJS implementation to replace existing sliders on my website. The goal is to have variable-width slides, showing a landscape slide in the center with a glimpse of the preceding and following slides on each side. If it's a portrait slide, I want to show a bit more. Images might clarify what I mean.

My current setup for SwiperJS only displays one image at a time. I'm looking to adjust it so that the previous and next slides are partially visible as well. I believe it's just a small tweak away from where it should be, but I'm having trouble figuring it out.

<!DOCTYPE html>
 <html lang="en">

 <head>
  <meta charset="utf-8">
  <title>Swiper demo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">

  <!-- Link Swiper's CSS -->
  <link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css">

  <!-- Demo styles -->
  <style>
    html,
    body {
      position: relative;
      height: 400px;
    }

    body {
      background: #eee;
      font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
      font-size: 14px;
      color: #000;
      margin: 0;
      padding: 0;
    }

    .swiper-container {
      width: 100%;
      height: 100%;
      background-color:#ffcc00;
    }

    .swiper-slide {width:100%;height:100%;}

    .swiper-slide img {
      height:100%;
    }
   </style>
 </head>

 <body>
   <!-- Swiper -->
   <div class="swiper-container">
     <div class="swiper-wrapper">
       <div class="swiper-slide"><img src='https://via.placeholder.com/900x600.png?text=900x600'></div>
       <div class="swiper-slide"><img src='https://via.placeholder.com/600x900.png?text=600x900'></div>
       <div class="swiper-slide"><img src='https://via.placeholder.com/900x600.png?text=900x600'></div>
       <div class="swiper-slide"><img src='https://via.placeholder.com/900x600.png?text=900x600'></div>
       <div class="swiper-slide"><img src='https://via.placeholder.com/600x900.png?text=600x900'></div>
       <div class="swiper-slide"><img src='https://via.placeholder.com/900x600.png?text=900x600'></div>
       <div class="swiper-slide"><img src='https://via.placeholder.com/600x900.png?text=600x900'></div>
       <div class="swiper-slide"><img src='https://via.placeholder.com/600x900.png?text=600x900'></div>
       <div class="swiper-slide"><img src='https://via.placeholder.com/600x900.png?text=600x900'></div>
       <div class="swiper-slide"><img src='https://via.placeholder.com/600x900.png?text=600x900'></div>
    </div>
    <!-- Add Pagination -->
    <div class="swiper-pagination"></div>
 <div class="swiper-button-prev" tabindex="0" role="button" aria-label="Previous slide" aria-     controls="swiper-wrapper-181ef273d22e62b1"></div>
  <div class="swiper-button-next" tabindex="0" role="button" aria-label="Next slide" aria-     controls="swiper-wrapper-181ef273d22e62b1"></div>
  </div>

  <!-- Swiper JS -->
  <script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>

  <!-- Initialize Swiper -->
  <script>
    var swiper = new Swiper('.swiper-container', {
      slidesPerView: 'auto',
      spaceBetween: 30,
      loop: true,
      loopFillGroupWithBlank: true,
      pagination: {
         el: '.swiper-pagination',
        clickable: true,
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    });
  </script>
</body>

</html>

Here's an example of a landscape image within the slider, showcasing partial slides on either side

And here's an example of a portrait image within the slider, displaying partial slides on either side

Answer №2

Setting up a new Swiper in Javascript:
new Swiper('.swiper-container', {
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        slidesPerView: 2.5,
        spaceBetween: 30,
        watchSlidesVisibility: true,
    });
    
CSS for the swiper slides:
.swiper-slide {
  padding-block: 110px;
  text-align: center;
  font-size: 18px;
  background: #ccc;
}
Loading Swiper libraries and setting up HTML structure:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.1/js/swiper.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.1/css/swiper.min.css" rel="stylesheet"/>
<div class="container">   
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">Slide 1</div>
            <div class="swiper-slide">Slide 2</div>
            <div class="swiper-slide">Slide 3</div>
            <div class="swiper-slide">Slide 4</div>
            <div class="swiper-slide">Slide 5</div>
            <div class="swiper-slide">Slide 6</div>
        </div>

        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Implement Drupal.behaviors for Specific Pages?

Currently, I have a module that showcases an alert to users within a block. For those interested, you can find my code on GitHub here: https://github.com/kevinquillen/User-Alerts If you would like more information about the module and its functionality, ...

What is the syntax for creating a zip function in TypeScript?

If I am looking to create a zip function: function zip(arrays){ // assume more than 1 array is given and all arrays // share the same length const len = arrays[0].length; const toReturn = new Array(len); for (let i = 0; i < len; i+ ...

Achieving a full-screen div or video that remains fixed in a specific position on the browser window can be done by following these steps

I am seeking guidance on how to create a video or div that remains fixed in a specific position while adjusting its size to always match the browser window. You can see an example of what I am aiming for here. The website mentioned above contains a video ...

How to extract the value of a key from JSON using JavaScript

Need help with an API call to retrieve a list of subcategories? Here's an example of the JSON format: { "description": "Flower", "name": "Flower", "parent_id": "1" }, { "description": "Moon", "n ...

Implementing jQuery autocomplete on dynamically generated fields post page load

I have successfully implemented jQuery UI Autocomplete to fetch data from a DB and populate a form dropdown. However, I am faced with the challenge of adding more form fields dynamically that also need to have the autocomplete feature applied to them. Is ...

struggling to develop a sophisticated 'shopping cart organization' program

I am in the process of creating a database for video spots, where users can view and modify a list of spots. I am currently working on implementing a cart system that automatically stores checked spot IDs as cookies, allowing users to browse multiple pages ...

Creating a unique bullet style using CSS

I've got a collection of student links that take users to their profiles, and I want to replace the usual circular bullet points with smiley faces. Each picture is 14x14 pixels in size. How can I use CSS to achieve this effect for the bullets? ul { ...

Narrow down JSON data by chosen element

Utilizing the autocomplete feature, I have created a system to display options based on a JSON file. My current objective is to filter the JSON data according to the selected item. For example, if I select "Roma" from the autocomplete input, I want to dis ...

How can I switch the visibility of two A HREF elements by clicking on one of them?

Let me break it down for you in the simplest way possible. First off, there's this <a href="#" id="PAUSE" class="tubular-pause">Pause</a> and then we have a second one <a href="#" id="PLAY" class="tubular-play">Play</a> Al ...

The functionality of iScroll becomes unresponsive once I implement Ajax to load the list

I have successfully implemented an iScroll list that works perfectly when coded directly into the HTML. However, when I attempt to use jQuery-Ajax to dynamically load the same list, it remains stuck at the top and refuses to scroll down to the bottom. $( ...

Can you tell me the name of the Javascript "protocol" or "custom" being referred to here?

From what I can gather, the requests array seems to consist of functions with unique formatting and syntax. However, I'm struggling to find relevant search terms to help me better understand it: var requests = { rewardPoints: function(cb) { io ...

Unraveling exceptions in Node.js akin to handling them in Java

I'm seeking to develop a node application and I need guidance on exception handling. In Java, we utilize the exception class for this purpose. How can I achieve something similar in node? Are there any libraries available specifically for handling exc ...

Utilize Express.js routes to deliver static files in your web application

I am looking to serve a collection of HTML files as static files, but I want the routes to exclude the .html extension. For example, when someone visits example.com/about, I'd like it to display the contents of about.html In my research on serving ...

Ways to display or conceal text using Vanilla JavaScript

I am struggling to toggle text visibility using Vanilla JS. Currently, I can only hide the text but cannot figure out how to show it again. <button id="button">my button</button> <div> <p id="text">Hello</p& ...

What steps can I take to properly position my child element within its parent container?

Is there a way to adjust the width of a child element so it fits snugly inside its parent div? The width should be contained within the padding area. The top and left placement is correct, but the right side needs adjustment. Check out this link for refe ...

AngularJS provides users with the ability to access additional information by matching specific identification

Is there a simple way in Angular to display the label of an item from an array without looping through it? I have an array of color options: $scope.colors=[ {id:"0",label:"blue"}, {id:"1",label:"red"}, {id:"2",label:"green"} ] And my data object store ...

How can I incorporate a material-ui component using innerHTML?

Hi there, I'm new to using React and StackOverflow. I've been attempting to incorporate a Button component from material-ui by utilizing document.getElementById.innerHTML. However, upon the appearance of the button, the material-ui styling fails ...

What could be the reason for the error message when using rs.send with a string input?

I'm a beginner in node and express, and I was trying to create an HTML form that takes two numbers and displays the result using "express" and "node". However, when I use rs.send(result), I encounter the following error: This error message appears on ...

Simple and quickest method for incorporating jQuery into Angular 2/4

Effective ways to combine jQuery and Angular? Simple steps for integrating jQuery in Angular2 TypeScript apps? Not sure if this approach is secure, but it can definitely be beneficial. Quite intriguing. ...

Having trouble with the second jQuery AJAX request not functioning properly?

I'm having trouble figuring out where I'm going wrong. The second ajax call isn't functioning properly, unlike the first one. $(".payment").click(function(){ visitorID = $(this).attr('id'); alert("Modal says "+visit ...