Simultaneously transitioning two full-width elements by sliding them in and out

Currently, I am working on creating an animation effect where one element slides out of the screen on the left while fading out to 0 opacity. Simultaneously, a new element slides in from the right to take its place.

Although the visual aspect is functioning properly, I am encountering a side effect where the window width extends beyond the screen, resulting in a horizontal scroll bar appearing.

To see a demonstration with all the code included, you can visit this CodeSandbox link: https://codesandbox.io/s/react-spring-carousel-forked-9wjjob?file=/src/carousel.jsx

The transitions for the animation are set up as follows:

const transitions = useTransition(items, {
    from: {
      opacity: 0,
      transform: `translateX(100%)`,
      width: "100%"
    },
    enter: {
      opacity: 1,
      transform: "translateX(0%)",
      width: "100%"
    },
    leave: {
      opacity: 0,
      transform: "translateX(-100%)",
      width: "0%"
    }
});

How can I resolve this side effect that is causing the window width to expand unnaturally?

Alternatively, is there another approach I can take to achieve the same visual effect without encountering this issue?

Answer №1

Follow these steps to adjust the wrapper element style:

  • Ensure relative positioning so that child elements are positioned in relation to it, not another ancestor.

  • Hide any overflow.

  • Adjust height to accommodate children, even with a 2rem top position.

    {{ height: "8rem", width: "100%", position: "relative", overflow: "hidden" }}
    

See Demo

You may need to make further adjustments for your desired layout, but this should resolve the scrolling issue.

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

Exploring the method of inserting an element into a nested array using JavaScript's ES6 features

Let's say I understand how to use the spread operator to add an element to a regular array in ES6. const arr = ["a","b"] const newArr = [...arr, "c"] This would result in ["a","b","c"] But when it comes to implementing this with a nested array, like ...

Determining height of an element in AngularJS directive prior to rendering the view

As I dive into the world of Angular, any assistance is greatly welcomed. My goal is to vertically align a div based on screen size (excluding the header and footer) when the page loads, the window resizes, and during a custom event triggered by the user ex ...

Tips for filling in the empty space next to an image with a negative left margin using text

Our goal is to create a layout where an image overflows from its container, leaving space for text to fill in the gap created by the offset image on the left side. https://i.sstatic.net/2dvLa.jpg Currently, our structure looks like this: <div class=" ...

Place CSS elements in relation to the underlying element

This is an example of my HTML structure: <nav id="menu"> <ul> <li><a href="">Products</a></li> <li><a href="">Offers</a></li> <li><a href="">References</a>& ...

I have exhausted all possible solutions to fix the issue with res.cookie, but unfortunately, it still does

I'm having trouble viewing cookies in my browser that are set by using res.cookie. Below is the code I am using, but for some reason the cookies are not being set. Can someone help me figure out how to properly set them? const token = generateToken(u ...

Steps to modify the border width upon gaining focus

I am struggling to adjust the border width when my input box is focused. Currently, it has a 1px solid border which changes to a 2px different color solid border upon focus. However, this change in border width is causing the containing div to shift by 1px ...

Tips for positioning a mat-form-field beside an h1 tag

I've been attempting to place an h1 next to a mat-form-field from Angular Material, but I'm encountering some difficulties. Here's what I've attempted so far: <div class="mat-elevation-z8"> <mat-form-field> <mat-l ...

The nested mapping operation produces no output

I am currently working on a React.js Dashboard project using MUI. In this project, I have a list of courses and athletes. Each athlete can be enrolled in multiple courses. For each enrolled course, I want to display a Card showing the course name and venu ...

Replacing the tbody element in React with centered text using inline styles ---If you want

I am working with an empty array in React that I translate into state. When the array is empty, I want to insert a text that says "no match events yet..." into a react-bootstrap Table's tbody. In the current setup, I am struggling to center the text ...

Issues with React Router's <Redirect> component functioning incorrectly

Below is the function responsible for returning my routes: ... import { Route, Switch, Redirect } from "react-router-dom" import { ApolloProvider } from "@apollo/client"; import { MuiThemeProvider } from '@material-ui/core/styles&a ...

What is the best way to align the items in my navigation bar to the center?

I'm having trouble getting my navbar buttons to appear in the center of the navigation bar. No matter what I try, they only seem to align to the left or right. Can anyone help me figure out how to center them? Here's the CSS code I'm working ...

Building a stack of DIVs, some with fixed heights and others without, potentially using only CSS

Hello everyone! I am having trouble deciding on the best approach to achieve a layout like the one below: +------------------------------------------------------------+ | div 0 , varying height +---------------------------------------------------------- ...

When an option is selected on the navigation bar, side bars appear for additional navigation

How can I remove the bars on the side of the navigation bar when hovering over it? I don't want to alter the appearance or position of the navbar itself, just the parts that are affected by hovering over it. HTML: <!DOCTYPE html PUBLIC "-//W3C//D ...

Navigate through AJAX Live search using arrow keys and mouse controls

I have encountered an issue with the autocomplete feature on my webpage. Although I am able to input suggestions, I am unable to navigate through them using the mouse pointer initially. Oddly enough, if I press the up/down keys once after the list items ar ...

A guide to adjusting the size of a mat-button using an svg mat-icon

I have PrimeNg and Angular Materials buttons on the same td. I am attempting to resize my mat-buttons to match the size of my pButtons but they are not adjusting properly. Should I consider using a different type of button with my icon? HTML <button ma ...

Having two ng-click events and two distinct classes in an ng-repeat based on the selected option

There are two buttons in my code that should remove a div within an ng-repeat loop. Depending on which button is clicked, a custom CSS class should be added to the effect. The CSS changes based on the option selected. When I click on a button, either &apo ...

What methods are available for testing asynchronous calls in React Redux?

I've been attempting to test the login user actions, but I keep receiving an empty array when calling store.getActions(). How can I properly test the login user function to receive the action type SET_USER upon success or ERROR if it fails? Below is t ...

Dropdown of ui-select in AngularJS is positioned behind a div element, causing

The ui-select-match and choices component seems to be hidden behind the div/form structure. This particular ui-select component is enclosed within several parent elements, including some divs and forms. It eventually renders as follows: > <ui-select ...

Is this loader going through a triple loop?

<style> .loader { position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 9999; background-repeat: no-repeat; background: url('images/page-loader.gif'); } </style> <script src="//ajax.googleapis.com/ajax/libs/jque ...

Working condition may vary depending on the size of the item

I have integrated the Masonry jQuery plugin into my design, but it is not functioning properly. The CSS class mentioned in the documentation only includes: width: 25% For my purposes, I modified it to: width: 24% float: left margin-right: 5px This modi ...