9

I want to show part of the second slide item, but don't know how to do it. This is what I have so far (basic Ionic slides):

<ion-slides pager >
            <ion-slide>
              <h2>Slide 1</h2>
            </ion-slide>

            <ion-slide>
              <h2>Slide 2</h2>
            </ion-slide>

            <ion-slide>
              <h2>Slide 3</h2>
            </ion-slide>
</ion-slides>

and this is how I want it to be: enter image description here

I've tried to change the width of the ion-slide to less than 100%, but the second and third slides just gets moved further to left, out of the screen.

Can anyone help me ?

Christer
  • 2,746
  • 3
  • 31
  • 50

8 Answers8

29

Edit:- For Ionic 5 Set the Options in the TS file.

slideOpts = {
  slidesPerView: 1.5,
  spaceBetween: 10 
};

For Ionic 3

I think you get it. This solution for the others who are still searching for this view. Just add this to the ion-slides tag.

slidesPerView="1.5" spaceBetween="10"
Ajoy Karmakar
  • 671
  • 7
  • 14
6

In UX, it is better for the user to know if he has more items to see, especially when we don't intend to use a pager.

ion-slides use internally http://www.idangero.us/swiper/ may this help us to accomplish that.

We can use slidesPerView="2" spaceBetween="250"

You have to refine the spaceBetween value according to your item slide size.

Lastly, you need an empty ion-slide in the end, because spaceBetween make a little mess in the positioning.

.card {
    width: 300px;
}

<ion-slides slidesPerView="2" spaceBetween="250">
    <ion-slide *ngFor="let foo of bars">
        <my-item class="card"></my-item>
    </ion-slide>
    <ion-slide>
        <!-- need a empty slide to avoid last item to be inaccessible -->
    </ion-slide>
</ion-slides>
Rob
  • 14,746
  • 28
  • 47
  • 65
4

After wrestling with this issue, I found a pretty straightforward solution that seems to work.

<ion-slides spaceBetween="-18">

Obviously, adjust the number to match your specific needs.

user4650164
  • 99
  • 1
  • 9
  • I think this is a pretty bad solution. Diego's suggestion is definitely better, and certainly this is the right way. The solution to the problem is called slidesPerView. If you want, you can use of course spaceBetween additionally – peter70 Nov 10 '17 at 08:47
3

I had some good results setting the slidesPerView to 'auto' and giving each slide a margin-right of -18px and a margin-left of 18px (except for the first and last, of which either the margin-left and margin-right are set).

Here's the template code:

 <ion-slides [slidesPerView]="'auto'">
     <ion-slide *ngFor="...">
         ...
     </ion-slide>
 </ion-slides>

And here's the css:

  ion-slide {
    width: 240px !important;
    height: 290px !important;

    margin-right: -18px;
    margin-left: 18px
  }

  ion-slide:first-child {
    margin-left: 0;
  }

  ion-slide:last-child {
    margin-right: 0;
  }
David Bulté
  • 2,988
  • 3
  • 31
  • 43
3

I got the solution,

    <ion-slides pager="false" slidesPerView="1.2" spaceBetween="10" centeredSlides=true loop=true>

  <ion-slide>
    <h1 class="card">1</h1>
  </ion-slide>

  <ion-slide>
    <h1 class="card">2</h1>
  </ion-slide>

  <ion-slide>
    <h1 class="card">3</h1>
  </ion-slide>

</ion-slides>
Upendra
  • 683
  • 6
  • 23
2

IONIC 5

For similar type of scenario:

enter image description here

.ts

 slideOpts = {
    initialSlide: 1,
    speed: 400,
    slidesPerView: 1.2,
    spaceBetween: 10,
    centeredSlides: true
};

.html

<ion-slides [options]="slideOpts">
        <ion-slide>
          <img src="https://via.placeholder.com/300x200" alt="" class="slider-img">
        </ion-slide>
        <ion-slide>
          <img src="https://via.placeholder.com/300x200" alt="" class="slider-img">
        </ion-slide>
        <ion-slide>
          <img src="https://via.placeholder.com/300x200" alt="" class="slider-img">
        </ion-slide>
    </ion-slides>
Najam Us Saqib
  • 3,190
  • 1
  • 24
  • 43
0

From Ionic 3, you can use slidesPerView="2.5" spaceBetween="10" in ion-slides tag , the slidesPerView can have any decimal values with single decimal digit. Adjust accordingly.

ArtOfCode
  • 5,702
  • 5
  • 37
  • 56
0

I managed to get this view , in ionic3. It's a piece of cake. All you need is to set slidesPerView= "auto" then in your sass

ion-slide {
        width:80% !important;
        margin-top: 0px;}

see the view

kenlukas
  • 3,616
  • 9
  • 25
  • 36