Shifting the pagination outside of the slider in ReactJS and SwiperJS seems to be tricky. Despite trying to adjust the margins and padding, the pagination

I've been struggling with this issue for a while now. I need to move the pagination outside of the slider, but using padding or margin doesn't seem to work. It always remains inside the slider, and if I try to position it outside, it gets hidden.

Check out my code on GitHub

import React from "react"

import SwiperCore, {
  Navigation,
  Pagination,
  Scrollbar,
  A11y,
  Keyboard,
  Mousewheel,
  EffectCoverflow,
} from "swiper";

import { Swiper, SwiperSlide } from "swiper/react";

// Import Swiper styles
import "swiper/swiper.scss";
import "swiper/components/navigation/navigation.scss";
import "swiper/components/pagination/pagination.scss";
import "swiper/components/scrollbar/scrollbar.scss";
import "./Simple.css";

// install Swiper components
SwiperCore.use([
  Navigation,
  Pagination,
  Scrollbar,
  A11y,
  Keyboard,
  Mousewheel,
  EffectCoverflow,
]);

const SimpleSwiper = () => {
  const params = {
    effect: "coverflow",
    grabCursor: true,
    centeredSlides: true,
    slidesPerView: "auto",
    coverflowEffect: {
      rotate: 40,
      stretch: 0,
      depth: 100,
      modifier: 1,
      slideShadows: true,
    },
  };
  return (
    <div className="swipebody">
      <Swiper
        {...params}
        spaceBetween={50}
        slidesPerView={5}
        navigation
        pagination={{
          clickable: true,
          renderBullet: (index, className) => {
            return '<span class="' + className + '">' + (index + 1) + "</span>";
          },
        }}
        keyboard={true}
        mousewheel={true}
        // scrollbar={{ draggable: true }}
      >
        <SwiperSlide className="swiper-slide">
          <img src="https://picsum.photos/id/237/250/200" />
        </SwiperSlide>
        ...
      </Swiper>
    </div>
  );
};

export default SimpleSwiper;

CSS

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    position: relative;
}

.swipebody{
    margin-top: 50px;
    width: 100%;
    height: 100%;
    position: relative;
}
...

I tried using bottom: -10px but it remains hidden

Answer №1

To customize your pagination configuration, you can specify a custom container element:

pagination={{
  el: '.my-custom-pagination-div',
  clickable: true,
  renderBullet: (index, className) => {
   return '<span class="' + className + '">' + (index + 1) + "</span>";
  },
}}

Then simply insert

<div class="my-custom-pagination-div" />
wherever you choose.

This method of targeting elements may not be the traditional React/JSX approach, but it is currently the most effective solution available.

Answer №2

If relocating the pagination is your goal, consider placing it outside of the Swiper container for a simpler solution rather than implementing hacks to display it outside.

Alternatively, adjust the swipebody height to accommodate the pagination within the div but below the slides.

To explicitly set a height, try using height: 60vh instead.

Avoid using !important as it can be considered a hacky approach.

You may also experiment with setting a Z-Index (https://www.w3schools.com/cssref/pr_pos_z-index.asp) on the pagination element.

Given your current setup,

Answer №3

To see the effect, remove the box-sizing: box-border declaration from the * setting in your CSS (you may need to narrow down where you make this change, but start here).

By using box-sizing: box-border, any margins or padding applied to an element will be included in its dimensions - not placed outside of it. It seems like you want these properties to affect the positioning of the element rather than its size.

Answer №4

While working with react, I managed to pinpoint the issue at hand. It turns out that the default positioning of the bullets is set to position: absolute, causing them to shift to the bottom using top. This results in the bullets overlapping the content when the swiper content reaches the bottom. To resolve this, simply increase the height of the swiper container, which will effectively move the bullets downwards.

To accomplish this, assign a class to the swiper and specify its height like so:

React

<Swiper className="swiper-container">
    //swiper slides here
</Swiper>

CSS

.swiper-container{
         height: 500px;
    }

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 need assistance from someone knowledgeable in HTML and CSS. I am trying to create a div that dynamically increases its width until it reaches a specific

Seeking assistance to create a dynamic div that continuously expands its width until it reaches a maximum of 540px. It should start at 75px. Below is the HTML and CSS code I've attempted: .Loading-Screen { background-color: black; color: alicebl ...

When using React router, I'm experiencing a redirection issue where instead of being directed to the /login route,

I am currently developing a web application with an authentication system. I have encountered an issue when logging out. Instead of redirecting me to the /login page, it redirects me to /[object%20Object]. Can anyone help me figure out what might be causin ...

How can we dynamically navigate to the next line within the Material UI DataGrid component when space is limited?

Currently, I am working with the datagrid component from material ui. I have retrieved some data from a database and am attempting to pass it to the datagrid component. However, I have noticed that certain fields contain long strings which do not fully app ...

Integrate Semantic UI Build into your Webpack configuration

In my current project, I am incorporating Semantic UI with React and utilizing Webpack for the build process. Below is my Webpack configuration: module.exports = { entry: [ './src/index.js' ], output: { path: __dirname, public ...

What is the best way to make the current tab stand out?

I have implemented a TabHeader component to create a dynamic Tab Menu that displays table contents based on months. The loop runs from the current month back to January, and the content is updated dynamically through an API call triggered by changes in the ...

An unanticipated SyntaxError was encountered while attempting to utilize an Ajax post, specifically in relation

I can't seem to figure out how to troubleshoot an ajax/jquery error. Here is the function I'm working with: var LogIn = { Email: $("#Name").val(), MobileNo: $("#txtMobileNumber").val(), PinCode: '', ...

Comparing two tables in jQuery/Javascript for matching data

I want to check for matches between the rows of two HTML tables, where the data in the first cell can be duplicated but the data in the second cell is always unique. The goal is to determine if the combination of values in the first and second cells of tab ...

Exploring the Power of Two jQuery Ajax Functions in Your Script

Is it possible to provide guidance on how I can successfully execute two separate Ajax calls within a single script without encountering conflicts? A sample of the current script structure is as follows: <script type="text/javascript"> $(document).r ...

What is the best way to conceal a set of buttons on the main page using vue.js?

I'm having trouble hiding a button-group on the main page. It should disappear when either the button moveTo(0) is clicked or when scrolling to the top of the page. show: function() { this.scrollTop = (window.pageYOffset !== undefined) ? windo ...

How can I customize the hover effect of the drop-down options <option> to have a "blue" background color?

I've been struggling to change the default blue background color of the drop-down options when hovering over them with the mouse. Despite trying various solutions from this forum, nothing seems to be working at the moment. (These solutions used to wor ...

After successfully logging in with Firebase, the login page still loads on index.tsx upon page refresh

I am currently working on a Next.js project with Firebase as the backend database. Within this project, there are multiple pages such as visitors.tsx, issues.tsx, and index.tsx (which serves as my login page). Upon logging in to the application, I am direc ...

Is there a way to modify the color of ASCII art letters within HTML's <pre> tags?

For this illustration, I aim to transform the letter "y" into a shade of blue. ,ggggggggggggggg dP""""""88""""""" Yb,_ 88 `"" 88 88 88 gg gg 88 I8 8I gg, 88 ...

There was a TypeError encountered while attempting to read the 'useState' properties of null for the swiper and next.js 13 modules

We recently updated to next.js 13 and encountered the following error message: TypeError: Cannot read properties of null (reading 'useState') The error stack trace is displayed below: Warning: Invalid hook call. Hooks can only be called inside ...

Why is access to fetch from the origin localhost blocked by CORS policy, while posting using ajax does not face this issue?

Let's dive into the difference between AJAX and Fetch without involving CORS. I want to understand why an AJAX POST request works flawlessly while a Fetch POST request fails! Currently, I am using jQuery and AJAX for both GET and POST operations. Whe ...

Is there a way to efficiently modify the positions of numerous markers on Google Maps while utilizing PhoneGap?

Hey there, I'm new to this and I have a service for tracking multiple cars. I'm using a timer to receive their locations, but I'm having trouble figuring out how to update the old marker with the new value. I've tried deleting all the m ...

React Material Design Cards for Responsive Layouts

Hi there, I am currently navigating my way through Material Design and attempting to ensure that the cards section is responsive by utilizing media queries and flex. However, I have encountered an issue where only a portion of the image is displayed when t ...

Combining Multiple Routes with Redirect using React Router

If I have an authentication token and a function called checkToken, how can I efficiently reroute from multiple routes using React Router to avoid repetition like the example below? <Route exact path="/" render={() => { return checkToken() ? (< ...

Converting a JavaScript function to work in TypeScript: a step-by-step guide

When writing it like this, using the this keyword is not possible. LoadDrawing(drawing_name) { this.glg.LoadWidgetFromURL(drawing_name, null, this.LoadCB,drawing_name); } LoadCB(drawing, drawing_name) { if (drawing == null) { return; ...

What might be causing my Vue unit test to overlook a function?

I am in the process of creating unit tests for my component dateFormat.js using Jest. The focus is on testing the function formatDateGlobal. Here is an excerpt from the test: import DateFormat from '../dateFormat'; describe('dateFormat.js& ...

Is it possible to resize an object using JavaScript?

Is it possible to change the size of an object when loading specific data by clicking on navigation? <object id="box" width="250" height="250" data=""></object> Although the JS code loads the data, it does not change the size. document.getEl ...