Is there a way to position a slide container so that it is initially 10% from the left margin, and then shifts to be 10% from the right margin when it reaches the end? (Using vue-awesome

I'm facing a bit of challenge with this question, and I hope someone can help me find a solution.

Currently, I am utilizing vue-awesome-swiper (). My goal is to achieve the following layout:

An initial left margin of 10%: https://i.sstatic.net/AoLhK.png

When inner slides are visible, they should stretch to full width from left to right: https://i.sstatic.net/Xg4qu.png

Upon reaching the last slide, there should be a 10% margin on the right: https://i.sstatic.net/sN08O.png

You can refer to examples here: .

To implement the initial margin, I added margin-left:10%; to the swiper-wrapper class. However, this causes the last slide to be covered as the content shifts by 10%. I would like the margin-left to be removed when the last slide is reached and vice versa.

I have explored using events(): reachEnd and reachBeginning, and attempted to apply them in the following way:

<template>
<div>
      <swiper
        class="swiper"
        :options="swiperOption"
        :reach-beginning="handleTransitionStart"
        :reach-end="handleTransitionEnd"
        :style="transitionStarted ? { margin: '0 0 0 10%' } : { margin: '0 10% 0 0' }"

      >
.....
</swiper>
</div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';

@Component
export default class InformationSection extends Vue {
  @Prop() 'reachEnd'!: any;
  @Prop() 'reachBeginning'!: any;

  swiperOption = {
    slidesPerView: 4,
    spaceBetween: 30,
    pagination: {
      el: '.swiper-pagination',
      clickable: true,
    },
  };

  transitionStarted: boolean = true;

  handleTransitionStart(): any {
    // eslint-disable-next-line no-console
    console.log('Started');
    this.transitionStarted = true;
  }

  handleTransitionEnd(): any {
    // eslint-disable-next-line no-console
    console.log('Ended');
    this.transitionStarted = false;
  }
}
</script>

<style scoped>

  .swiper-slide {
    width: 60%;
  }
  .swiper-slide:nth-child(2n) {
      width: 40%;
  }
  .swiper-slide:nth-child(3n) {
      width: 20%;
  }

</style>

However, the above code implementation does not add margin to the right at the end. Can anyone provide guidance on how I can achieve this desired outcome?

Answer №1

slidesOffsetAfter

slidesOffsetBefore

Reference: Check this link for more information on slides offset after parameter

if($('.swiper').length !== 0){
const swiper = new Swiper('.swiper', {
    loop: false,
    slidesOffsetBefore: 5, // Set slide offset before to 5px
    slidesPerView: 1.3,  // Show second slide partially at 1.3 times the normal size 
    centeredSlides: true // Center the slides
    spaceBetween: 10,
  }
});}

Answer №2

When I needed something similar, I ended up creating my own scroller component. However, I understand that solution may not work for everyone.

If you're looking for a cleaner approach, there is a workaround available.

The first slide comes with a margin

<swiper-slide style="margin-left:10%;">I'm Slide 1</swiper-slide>

while the last slide remains invisible

<swiper-slide style="opacity:0;"></swiper-slide>

Vue.use(VueAwesomeSwiper)
new Vue({
  el: '#vueapp',
  components: {
    LocalSwiper: VueAwesomeSwiper.swiper,
    LocalSlide: VueAwesomeSwiper.swiperSlide,
  },
  data: {
    swiperOptionA: {

      slidesPerView: 4,
      spaceBetween: 30,
      pagination: {
        el: '.swiper-pagination',
        clickable: true,
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev'
      }
    },
  },
  computed: {
    swiperA() {
      return this.$refs.awesomeSwiperA.swiper
    },
  },
})
.swiper-container {
  height: 300px;
  width: 100%;
}

.swiper-slide {
  text-align: center;
  font-size: 38px;
  font-weight: 700;
  background-color: #eee;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/css/swiper.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/js/swiper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c7a7969216d7b697f636169217f7b657c697e4c3f223c2238">[email protected]</a>/dist/vue-awesome-swiper.js"></script>

<div id="vueapp">
    <swiper ref="awesomeSwiperA" :options="swiperOptionA">
      <!-- slides -->
      <swiper-slide style="margin-left:10%;">I'm Slide 1</swiper-slide>
      <swiper-slide>I'm Slide 2</swiper-slide>
      <swiper-slide>I'm Slide 3</swiper-slide>
      <swiper-slide>I'm Slide 4</swiper-slide>
      <swiper-slide>I'm Slide 5</swiper-slide>
      <swiper-slide>I'm Slide 6</swiper-slide>
      <swiper-slide>I'm Slide 7</swiper-slide>
      <swiper-slide style="opacity:0;"></swiper-slide>
      <!-- Optional controls -->
      <div class="swiper-pagination"  slot="pagination"></div>
      <div class="swiper-button-prev" slot="button-prev"></div>
      <div class="swiper-button-next" slot="button-next"></div>
    </swiper>
    <br>
</div>

Answer №3

To achieve this specific effect, one method that worked for me was adjusting the overflow property of the .swiper-container by setting it to visible. I then nested this container inside a div with the necessary margin, while enclosing everything in an outer div with overflow: hidden.

<div style="overflow: hidden">
    <div style="margin: 0 1rem">
        <div class="swiper-container" style="overflow: visible">
            <!-- ... -->
        </div>
    </div>
</div>

Another potential approach could involve adjusting the width of the .swiper-container to be less than 100% and ensuring it is centered within its parent element, though I have yet to test this alternative.

Answer №4

The issue I encountered was a unique one, as it wasn't a percentage problem but rather simply 32px of margin space remaining. I managed to fix it by implementing the following CSS adjustments:

.swiper-wrapper {
  margin-left: 32px;
}

.swiper-wrapper .swiper-slide:last-child{
  padding-right: 32px;
}

By solely applying the margin-left to the .swiper-wrapper element, the last slide would be cropped by 32px. Therefore, I extended the width of the last slide by adding padding-right accordingly. Another method you could use is utilizing margin-right or :after selector to increase the width of the last slide.

Answer №5

Success! This solution worked perfectly for my project.
_SW - website's screen width
1440 - chosen screen width
200 - ideal slides offset before

const raw_offset = (_SW * 100) / 1440
const offset = (200 * raw_offset) / 100

Answer №6

Include slidesOffsetBefore and slidesOffsetAfter in your swiper configuration

const mySwiper = new Swiper('.swiper', {
    slidesPerView: 3.5,  
    slidesOffsetBefore: 16, 
    slidesOffsetAfter: 16, 
    spaceBetween: 16, 
    centeredSlides: true, 
  
});

Alternatively, you can use slides-offset-before and slides-offset-after attributes when using the swiper element.

<swiper-container slides-per-view="3.5" slides-offset-before="16" slides-offset-after="16" space-between="16" centered-slides="true">
  <swiper-slide> </swiper-slide>
</swiper-container>

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

I am currently facing the challenge of how to properly upload a font to my website

One issue that arose was the font displaying incorrectly, despite all connections being correct. After investigating the HTML and CSS source code, I found the solution on how to properly implement the font, which resulted in it being displayed correctly. I ...

Best method for reducing spacing between elements in Bootstrap

Utilizing Bootstrap v3 and AdminLTE, I am aiming to reduce the spacing between each content element. Simply adjusting margin-left and margin-right impacts the right margin of the far right content as well as the left margin of the far left content, which ...

How to center align a Bootstrap 4 Navbar using CSS

Having trouble center-aligning your Bootstrap 4 navbar in your Angular code base? You're not alone. I too wanted to reduce the gaps between menu items when transitioning from one to another. Below you'll find snippets of my HTML, TS, and CSS: M ...

The function screen.getByText is not available in this context

My experience with jest and react-testing-library has been smooth for the most part, but I encountered some challenges when transitioning to the screen > getByText/etc testing method. Test describe('test the dashboard when loaded', () => { ...

"Discovering a button press using the Gamepad API: A Step-by-Step Guide

I'm currently building a web page that can detect button presses on an Xbox controller and display a boolean value based on the pressed button. Right now, I have successfully managed to detect when a controller is connected and show it as a string. Ho ...

Error: Mocha is unable to process the function as it is undefined

While conducting a series of tests in Mocha, I frequently encounter the error "Fatal Error: undefined is not a function" with no accompanying stack trace. This issue typically originates from within my code dependencies rather than the test case itself, ma ...

What is the best way to assign or convert an object of one type to another specific type?

So here's the scenario: I have an object of type 'any' and I want to assign it an object of type 'myResponse' as shown below. public obj: any; public set Result() { obj = myResponse; } Now, in another function ...

Is there a way to make the delete button remove just one item from the local storage?

Is there a way to make the delete button on each item in a list remove only that specific item without deleting the others and also remove it from local storage? I have been trying to figure this out but haven't had any success so far. <div class ...

Issue with Backbone model not properly processing JSON response during POST save operation

I am facing an issue with saving my model to the server and returning the ID along with other attributes. It appears that the response is being accepted as a string instead of a JSON object, leading to the entire string being added. I am using the express ...

Angular TS class with an ever-evolving and adaptable style

Currently, I am working with angular 9. Does anyone know a way to dynamically change the CSS of a class in a component? .stick-menu{ transform: translate(10px,20px); } I am looking to dynamically adjust the position of x and y values. For example: .stic ...

Sliding a DIV using Jquery to occupy the top half of the page

I am attempting to create a functionality similar to what is found on Microsoft's Support Website. You can check it out here: (Visit Here) By clicking on any product listed there, a drop-down menu appears. (Please note that the purpose of this articl ...

Adjust the canvas size to fit its parent element ion-grid

I am currently working on an Ionic 3 component that displays a grid of images connected by a canvas overlay. I have utilized Ionic's ion-grid, but I am facing an issue with resizing the canvas. The problem is that I cannot determine the correct dimens ...

Tips for sending every individual string object within an array as post data via ajax

I have a scenario where I am dealing with dynamically generated text boxes. Each value from these text boxes needs to be stored in a JSON array when submitted. The approach I took was to push all the values into an array and upon submission, these values a ...

What is the best way to position text messages at the bottom of a chat window?

I am currently working on a chat window, and while setting the height to 100% works perfectly, I am facing difficulty in making the messages appear at the bottom. HTML: <div class="d-flex justify-content-center"> <div class="container containe ...

Utilizing MongoDB query for geoLocation with maxDistance parameter

Customer location: customerCoordinates: [83,24] stores: { id:1, location: {coordinates:[85,44]...} maxRadiusDelivery: 2000 //meters }, { id:2, location: {coordinates:[82,34]...} maxRadiusDelivery: 100 //meters } Query: db.wh.find({ 'locati ...

Eliminate the dark loading screen flash effect from HTML5 videos

I've been working on a website for an online game that I'm currently playing. I've added an HTML5 video in the header section, but there's a brief flash of black screen while it loads, specifically in Chrome (no issues in Internet Explo ...

What leads to the inability to utilize environment variables in this TypeScript app built with Vue 3?

Currently, I am developing a single page application utilizing Vue 3 and TypeScript. The main purpose of this app is to interact with an API. All the necessary information including the API's URL and key are stored in the 'src\env.js' f ...

The Ionic Image breaks upon publication and installation of the apk

Upon accessing my images using this method, I discovered that they are broken after being installed on an actual device. <ion-avatar item-left> <img src="../../assets/uploads/user.jpg"> </ion-avatar> <h2>{{user?.fullname}}</ ...

Improve your browsing experience by setting requirements before accessing websites

Currently on the lookout to create SCORM compliant materials for utilization in a Moodle 2.2 framework (a learning management system); sidelined by limited support for SCORM 2004 hindering page sequencing. Considering ditching SCORM for an array of standa ...

What is causing the Unhandled Promise Rejection error when using await with Promise.all?

This is the general setup of my code: (async () => { try { const asyncTasks = [] for (let i = 0; i < 3; i++) { await new Promise((resolve, reject) => setTimeout(resolve, 1000)) for (let j = 0; j < 3; j++) { async ...