What is causing my div to transition repeatedly from the bottom to the top instead of the center position?

Currently, I am using React and Tailwind CSS and facing an issue with the transition property. My goal is to have a div grow in width and height from the center until it covers the entire viewport when a specific state variable becomes true. The content inside the div should stay in place while only expanding.

The problem I'm encountering is that no matter what type of transition I attempt (such as scale or transitioning y and x), the div keeps expanding from the bottom to the top of the viewport.

Below is a snippet of my code:

<div
   className={`z-10 px-8 pb-6 pt-10 text-white transition-all duration-500 ${slideTransition ? "translate-y-[50%]": "translate-y-0"} 
${isFullscreen ? "absolute inset-0 transition-all transform origin-center duration-[1s] ease-in-out": "relative"}
${fullscreenTransition ? 'max-h-screen' : 'max-h-full'}`}
>

This is how I trigger the fullscreenTransition state change:

 const triggerFullscreenTransition = () => {
    if (isFullscreen) return true
    else return false
  }

While the state change seems to be functioning correctly, the transition effect causes the div to slide upwards instead of growing from the center. Any assistance would be greatly appreciated.

Answer №1

Instead of using translateY to move the element vertically, consider transitioning the height and width properties. Below is a demo in vanilla javascript and CSS showcasing this effect in full screen:

let red=document.querySelector('.red');
let green=document.querySelector('.green');

function translateY(){
  red.classList.toggle('translateY');
}
function transitionHW(){
  green.classList.toggle('transitionHW');
  red.classList.toggle('hide');
}
body{
  margin:0;
  width:auto;
  height:100vh;
  display:flex;
  justify-content:center;
  align-items:center;
}
.box{
  width:100px;
  height:100px;
}
.red{
  background-color:red;
  transition:transform 500ms;
}
.green{
  background-color:green;
  transition:height 500ms,width 500ms;
}
.translateY{
  transform:translateY(-100%);
}
.transitionHW{
  height:100vh;/*the new wanted height*/
  width:100vw;/*the new wanted width*/
  transition:height 500ms,width 500ms;

}
.hide{
  display:none;
}
<div class="box red">
  <button id="y" onclick='translateY()' >move Y</button>
</div>
<div class="box green">
  <button id="HW" onclick='TransitionHeightWidth()' >transition height & Width</button>
</div>

You can achieve the same effect by writing Tailwind utility classes although I'm not an expert in that specific area.

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

Using jQuery to create a flawless animation

I am currently working on an animation project, and I have shared my progress on jsfiddle. Below is the code snippet I have utilized: /* JavaScript: */ var app = function () { var self = this; var allBoxes = $('.box&apos ...

Creating various containers in React JS for different components

I am faced with the task of rendering multiple DOM elements from my JavaScript code. Imagine I have div elements like this: <div id="div1"><div> //Some Html tags <div id="div2"><div> //Some Html tags <div id="div3" ...

Navigate through previous and next siblings in jQuery until a difference is found

Here's the issue: I have a div with multiple siblings positioned before and after it. For example: <div id="parent"> <div class="invalid"></div><!-- break iteration here !--> <div class="operand"></div> <div cl ...

Error in React-router: Unable to assign value to the 'props' property as it is undefined

Currently, I am working on setting up routing with Meteor using the react-router package. However, I have encountered a specific TypeError: Here is a link to an image that provides more context: This is the code snippet from my main.js file: import Reac ...

Incorporating text overlays on images that are compatible with responsive websites is a top priority for me

This is a piece of HTML code that utilizes bootstrap 3 for design purposes. The challenge faced is related to positioning text over an image. When resizing the browser, the alignment of the text with the background gets disturbed. Assistance is needed in r ...

The function 'toBlob' on 'HTMLCanvasElement' cannot be executed in react-image-crop because tainted canvases are not allowed to be exported

Currently, I am utilizing the react-image-crop npm package for image cropping purposes. Everything works perfectly when I pass a local image as props to the module. However, an issue arises when I try to pass a URL of an image fetched from the backend - th ...

In order to comply with JSX syntax rules in Gatsby.js, it is necessary to enclose adjacent elements

I want to apologize beforehand for the code quality. Every time I attempt to insert my HTML code into the Gatsby.js project on the index.js page, I encounter this error: ERROR in ./src/components/section3.js Module build failed (from ./node_modules/gatsb ...

Experiencing difficulties launching Expo on the IOS simulator

I seem to be having an issue with Expo on my iOS simulator. When I try to run it, I get the message 'There was a problem running Expo' as shown in this screenshot of the iOS emulator. Oddly enough, everything works fine on the Android emulator. ...

Troubleshooting Next.js 14 Project's NextUI Components Integration Problem

Currently, I am working on a project that involves Next.js 14 and integrates NextUI for UI components. Unfortunately, I have run into an issue where the NextUI components are not behaving as expected. To provide more clarity, I have included two images: o ...

Dealing with Sideways Overflow Using slideDown() and slideUp()

Attempting to use slideUp() and slideDown() for an animated reveal of page elements, I encountered difficulty when dealing with a relatively positioned icon placed outside the element. During these animations, overflow is set to hidden, resulting in my ico ...

Updating a property in React by fetching data from API and storing it in the cache

Recently, I implemented nanoid to generate unique IDs for my NBA team stat tracker app. However, upon browser refresh, the fetch function generates new IDs for each team stored in the favorites list. This causes the app to fetch data again and assign a new ...

How to implement CSS styling for a disabled component

In my application, I have a FormControlLabel and Switch component. When the Switch is disabled, the label in FormControlLabel and the button in Switch turn gray. However, I want to maintain the color of both the label (black) and the button (red). To test ...

What is the best way to display text outside of the current div container?

When I include the "Y" in the code snippet below, $title1 and $title2 are positioned outside of the div and centered against the width of the page. However, without the "Y", the text shifts up and aligns with the address class instead. It's puzzling w ...

What's the best way to implement a font family in a React component?

I'm currently attempting to implement the font found at: https://fonts.google.com/specimen/Source+Code+Pro After downloading the font into my directory, it appears as shown in this image: enter image description here This is how I have it set up: &l ...

Creating dynamic forms with JQuery and ReactJS

I have been tasked with creating a form-builder that will be integrated into an application. My role is to focus on designing the front-end using ReactJS. The client’s requirements are very similar to the features of the "jQuery Form-Builder," so I decid ...

What is the process for assigning a serial number to each row in the MUI DataGrid?

Initially, the server is accessed to retrieve some data. After that, additional data is added. While the data does not contain an ID, the form must still display a serial number. const columns: GridColDef[] = [ { field: 'id' ...

An issue with jQuery and LESS causing stylesheets to update only once

Struggling with updating a custom CSS href - everything works perfectly the first time, but subsequent updates do not reflect any changes on the page. Even though I can see the href updating in Chrome debugger and there are no errors being thrown by LESS, ...

Guide on verifying the global fetch response in a JEST test scenario

I've been utilizing jest to test my node function that makes a fetch() call to an API for data retrieval. However, while trying to write the test cases, I encountered an error message: expect(received).resolves.toEqual() Matcher error: received ...

The React app I've been working on has a tendency to unexpectedly crash when reloading the code from time

Dealing with a frustrating issue here. I've been working on an app and for the past few weeks, it keeps crashing unexpectedly. It seems to happen more frequently after saving my code to trigger a reload, but it can also just crash randomly while navig ...

Developing a system mode called "night mode"

I've decided to incorporate a dark mode feature into my Wordpress theme. Creating both dark and light modes was a breeze, but now I want to add a third mode that serves as the default for pages. This new mode will automatically switch between dark a ...