Swiper is encountering issues with resizing images correctly while the sidebar is in view

I'm facing an issue with incorporating the Swiper slider on my webpage. While I have a sidebar displayed, the Swiper image does not resize properly when the screen resolution is set to 1680x1050. The image gets cut off, but when I hide the sidebar, the entire slide is displayed correctly.

For reference, you can view a demo at:

In the demo, there is a section with the Swiper slider and another section with an image and text where resizing works seamlessly - this is exactly what I am aiming to achieve with Swiper.

I have attempted adjusting settings in the main.css file, but unfortunately, I have not had any success in resolving the issue. Would someone be able to assist me with this problem? Thank you very much!

Answer №1

I'm not entirely sure if I understand, but the only way I can view a cropped image is when I navigate to the website by typing the following address in my browser: .

However, when I refresh the page, the image is no longer cropped. This could be related to the implementation of the Swiper in your ready/load events without access to your Javascript, it's hard to pinpoint the exact issue.

Upon further investigation... It seems like the Swiper initializes before the menu is opened, causing it to use 100% of the window width.

On the other hand, when I refresh the page, the image is not cropped because it dynamically adjusts the width based on window.width - menu.width. However, when I click on the menu, the slider shows part of the next slide, indicating that the Swiper is initialized after the menu is opened.

To resolve this, you may need to reinitialize the Swiper every time the menu is opened or closed, similar to how it behaves when the window is resized. Perhaps calling mySwiper.updateSize() in your Javascript file whenever these events occur could solve the issue.

Update:

I suggest moving the initialization logic to your Javascript file and calling mySwiper.updateSize() whenever the window is resized or the menu is toggled. This approach might resolve the cropping issue.

Update 2:

Finally, using mySwiper.update() instead of mySwiper.updateSize() at the end of the process seemed to work effectively.

Answer №2

Encountered the same problem, but managed to find a solution using React Hooks. Hopefully this will assist someone else.

Start by importing swiper:

import SwiperCore, { Autoplay, Navigation, Pagination } from "swiper";

Initialize swiper instance using useEffect:

useEffect(() => {
    swiper = new SwiperCore(".swiper-container", {
      spaceBetween: 0,
      slidesPerView: 1,
      navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
      },
      updateOnWindowResize: true,
      loop: true,
      speed: 1200,
      pagination: {
        clickable: true,
        el: ".swiper-pagination",
      },
      autoplay: {
        delay: 4500,
        disableOnInteraction: false,
      },
    });
  }, []);

Also utilize another useEffect to observe the sidebar display. showLeftSideBar is a boolean parameter that changes based on the state:

useEffect(() => {
    if (swiper !== null) {
      swiper.update();
    }
  }, [showLeftSideBar]);

Showcasing the component code with Material UI:

<Box className={"swiper-container"}>
  <Box className={"swiper-wrapper"}>
      <SwiperSlide className={"swiper-slide"}>
        <Box>1</Box>
      </SwiperSlide>
      <SwiperSlide className={"swiper-slide"}>
        <Box>2</Box>
      </SwiperSlide>
      <SwiperSlide className={"swiper-slide"}>
        <Box>3</Box>
      </SwiperSlide>
  </Box>
  <Box className={"swiper-button-next"}>
    <ChevronRightIcon />
  </Box>

  <Box className={"swiper-button-prev"}>
    <ChevronLeftIcon />
  </Box>

  <Box className={"swiper-pagination"}></Box>
</Box>

This approach should resolve the issue at hand.

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

What is the best way to maintain scrollbars on mobile devices?

I'm currently building a cross-platform application using Angular 4 that needs to work seamlessly on both mobile and desktop devices. Is there a special trick to prevent the scrollbars from disappearing when viewing this page on a mobile browser? I&ap ...

Tips for dynamically resizing the content area using CSS without the need for javascript

What I need is as follows: The blue area should resize when the browser window resizes. The header must be visible at all times. The blue area should start right after the header ends, not overlapping or appearing above it. The blue area should end befor ...

Adjust the size of a div element after updating its content using Mootools

I am currently utilizing mootools 1.2 on a basic webpage. There is a div that contains a form (referred to as formdiv) which is submitted through JS/ajax. Once the ajax request returns a "confirmation message" data, the formdiv updates accordingly. The is ...

Tips for showing a table during onfocus event?

I have created an input text field with a label for a name. When I enter a name in the text box, it displays some related names using the onfocus event. Now, my goal is to display a table under the text box with the ID or NO and NAME when a name is entered ...

The navigation bar seems to be malfunctioning as it is unable to direct users

The navigation bar consists of links to HOME, ABOUT, WORK, and CONTACT. By using the href attribute, I can navigate through the page on this single page website. PROBLEM: The ABOUT link is not working. ATTEMPTS MADE: I checked for any typos but found non ...

I am looking to have the first option in the dropdown menu appear as a placeholder text

In my form, I have two phone number fields - one for mobile phone and the other for home phone. I need to make only one of them mandatory. Can someone please advise me on how to accomplish this? ...

Is it feasible to activate a Bootstrap Tooltip from a different element?

I am currently developing a system that enables users to add notes over an image they upload. Presently, I have a note div that, upon clicking, opens a text box for editing. When hovering over this div, I utilize Bootstrap Tooltip to preview the contents o ...

Any tips on showing inline input within an li element?

HTML Code: <div id="choices"> <ul> <li> <label for="input_size">Input Size</label> <input type="text" id="input_size"> </li> <li> ...

Executing AJAX function via a dropdown button

Utilizing a jquery-based dropdown menu from here. The dropdown is added to an existing button triggering an AJAX call that removes a row from the screen. The goal is to execute the AJAX call when any option in the dropdown is selected, instead of using th ...

How to effectively manage an overflowing navigation bar

I am currently facing a challenge with the main navigation bar on my website, specifically with how it handles an overflow of links on smaller screen resolutions. I have attempted to adjust the height of the navigation bar at smaller media queries, but I&a ...

execute ajax within a standalone javascript function

I am just starting to learn about jquery and ajax, and I have a specific requirement to call ajax from a separate javascript function. The issue is that the JSP file is dynamically generated and the button IDs in the JSP file are also created using a for l ...

What is the best way to locate a particular JSON key value pair through JavaScript?

I'm in the process of creating a unique app that forecasts the weather for an upcoming event based on the city location specified. To achieve this, I am utilizing two APIs - one API provides an array of objects representing each event by an artist ent ...

How can we adjust the flex values to ensure the label displays properly on a narrow container width?

The labels in the left column of the form are initially sized correctly, but shrink when there is not enough room. However, there's a discrepancy in label size between rows with 2 items and rows with 3 items. body { font-family: Arial; font-siz ...

Configure UL element as a dropdown on mobile devices and expand it to full width on desktop screens

Circumstances This HTML code is intended to create a horizontal <ul> element that spans the entire width of the <div class="list__wrapper"> container on desktop screens with a minimum width of 1024px. However, for mobile devices with a maximum ...

Can someone guide me on replicating this design with Bootstrap?

How do I achieve this layout using Bootstrap? https://i.sstatic.net/Ffvog.jpg I have checked my code but can't seem to find the mistake. Can someone please help me out? <div class="row"> <div class="img-5"> <div class="c ...

``Is there a reason why JavaScript/jQuery events are not functioning properly within a PHP foreach

Although I have a good understanding of foreach loop in PHP, I am facing a new challenge where the script within the foreach loop only works for the first item. This is puzzling to me and I'm trying to figure out why. In my code, I am retrieving an i ...

Each JSON document is returned as a single string rather than individual objects

I am currently implementing the jQuery autocomplete feature in Codeigniter, using @Phil Sturgeon's client rest library to fetch autocomplete data from Netflix. While I am able to retrieve and access the first element correctly with response(data.aut ...

What is the best way to conceal an HTML element on a particular page that is already styled as (visibility: visible), but only if its child has a specific class assigned to it?

I have a grid of content and posts displayed on multiple webpages. Users can select specific items from the grid to navigate to more detailed information on another page. Each webpage has its own grid which I want to filter based on a specific class. The ...

Different ways to automatically trigger a function in JavaScript

There are various ways to trigger a function automatically in javascript when a page loads. I am interested in knowing which method is considered the most effective and reliable. If you have a unique approach that differs from others, please share it here ...

Unable to decipher Material UI class names

I am experimenting with a React app using Material UI to explore different features. I'm attempting to customize the components following the original documentation, but making some changes as I am using classes instead of hooks. However, I keep encou ...