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

Browserify is having trouble locating the npm module

I'm encountering great difficulty while trying to develop an NPM module: react-smallgrid import React from 'react'; import _ from 'lodash'; export default class SmallGrid extends React.Component{ After compilation: browserify: ...

Implementing child components in React using TypeScript and passing them as props

Is it possible to append content to a parent component in React by passing it through props? For example: function MyComponent(props: IMyProps) { return ( {<props.parent>}{myStuff}{</props.parent>} } Would it be feasible to use the new compone ...

Error Encountered: An <Route> should always serve as a child element within a <Routes> container, not displayed independently. Kindly encase your <Route> within a <Routes> element

Encountering an error after implementing Private Route. The issue arises when attempting to access the Profile page which is nested in the application's structure. App.js import './App.css'; import { BrowserRouter, Route, Routes } from &ap ...

How can we create lists using parentheses specifically with Javascript or jQuery?

I am looking to create a formatted list using <ol> and <ul> tags with parentheses included. (1) List1 (2) List2 (3) List3 (4) List4 The challenge is to achieve this formatting purely with javascript or jQuery, without relying on any external ...

Issues with CSS not being applied when the page content exceeds the viewport

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>Unique Title</title> <style> #div1 { posit ...

CSS - Creating a Gradient Effect on div Borders for a Transparent Fade Effect across a Specified Distance

I am trying to achieve a fading effect for the background color of a div that contains a form. The solid background should fade out to transparent based on a pixel variable, allowing the background behind it to show through. This pixel value determines how ...

Testing a React component to ensure that it correctly handles clicks occurring outside

Utilizing the solution found in this particular answer to address clicking outside of a component: componentDidMount() { document.addEventListener('mousedown', this.handleClickOutside); } componentWillUnmount() { document.removeEventLis ...

The surprising outcomes of using Map and If statements together

I am striving to accomplish the following results: If the name entered already exists in the "persons" array, an alert should be displayed saying "Name already exist". The code seems to have a flaw as I am consistently receiving the alert message. I susp ...

Invoking two asynchronous functions in React, where the first function relies on the state modified by the second function

Currently, I am working on an app that utilizes the Geoapify API. Within this app, I have implemented three primary API functions. Users are presented with two options on the user interface: they can manually search for cities or utilize their current loca ...

The requested module cannot be located, were you referring to "js" instead?

I am currently developing a React application using webpack and typescript. I have integrated the dependency react-financial-charts into my project, and it is properly specified in the package.json. Inside the node_modules directory, there are two folders ...

Tips for customizing the appearance of Material UI's time picker diolog styles

I am currently incorporating the Material UI Time Picker from GitHub, specifically TeamWertarbyte's repository. My task involves modifying the color of the time displayed. In the image provided, I aim to customize the color of the time shown as 11:3 ...

Guide on Triggering a Custom Popup Message upon a Server Action in Next.js

Hey there, I'm currently facing some difficulties in finding the proper way to trigger a toast notification after successfully mutating data using server actions in Nextjs 13. The toast component I'm working with is specifically designed for clie ...

What could be causing my Material UI Table to disappear when I toggle the "Show" option?

I am incorporating the Grow material ui component to display or hide a table. Initially, the table is visible. <Box className={classes.object_controls_wrapper}> <Box sx={{ display: "flex" }}> <Grow in={objectViewOpened ...

Exploring the world of CSS and designing layouts using multiple div elements

Upon reviewing this website, I must admit it is getting close to what I envision. However, being a perfectionist and new to HTML and CSS, I find myself struggling to achieve the desired outcome. The layout consists of two divs named topred and top yellow, ...

How to show multiline error messages in Materials-UI TextField

Currently, I am attempting to insert an error message into a textfield (utilizing materials UI) and I would like the error text to appear on multiple lines. Within my render method, I have the following: <TextField floatingLabelText={'Input Fi ...

Troubleshooting AngularJS Directives' Cross-Origin Problems within Eclipse

Hello, I am facing an issue while using Angular JS in Eclipse. Specifically, when attempting to use Directives, I encounter a problem with the Cross-Origin Resource Sharing (CORS) policy when loading the Directives template in the index.html file. XMLHttp ...

Tips for resolving a sluggish webpage with database content

I am facing an issue with one specific page on my website that contains links to the database. The loading speed of this page is extremely slow and I'm looking for ways to speed it up. I have noticed that even when there are no visitors on the server ...

Adjusting image dimensions before delivery for a mobile site

On my desktop website, I currently use images with a size of 500*500. However, when it comes to the mobile version of my site, downloading such large images takes an excessive amount of time. Simply specifying width & height attributes in <img> doesn ...

Can you explain the exact purpose of npm install --legacy-peer-deps? When would it be advisable to use this command, and what are some potential scenarios where it

Encountered a new error today: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1cfc4d9d5d5d6c8cfe1918f908f91">[em ...

Issue with Axios code execution following `.then` statement

Recently diving into the world of react/nodejs/express/javascript, I encountered an interesting challenge: My goal is to retrieve a number, increment it by 1, use this new number (newFreshId) to create a new javascript object, and finally add this event t ...