英文:
Why is my container transition not smooth when grids get appended?
问题
I'm at a bit of a loss on my transition animation, I've replicated the problem in a codepen to share.
When I click the button, it creates two grids. Then applies a class to the bottom container to shift its Y position down the page. A setTimeout waits for this transition to end and then it appends these two grids to the main container.
But as you'll notice, after the animation has occurred it pushes the bottom container further down the page, and I can't figure out why. I asked chatGPT, but it wasn't able to come up with any potential solutions.
As I said, I've tried various ways to wait for the transitionend of the container, but it always seems to jump down, and I can't seem to get a seamless transition from its starting position to below the grids.
The related animation is happening on the transition-container style here:
.grid.left,
.grid.right {
display: flex;
transform: scale(0);
background: var(--main-background-clr);
border-radius: 5px;
flex-shrink: 0;
width: 50%;
height: 100%;
aspect-ratio: 1/1;
transition: 700ms ease;
}
.grid.left.visible,
.grid.right.visible {
transform: scale(1);
}
.transition-container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 20px;
width: 100%;
transition: all 0.5s ease;
}
.transition-container.shift-down {
transform: translateY(100%);
}
.ship-main-container {
display: flex;
flex-direction: column;
align-items: center;
padding-top: 10px;
gap: 20px;
width: 100%;
height: 200px;
background: var(--grid-border-clr);
}
This is the JavaScript code which generates the grid and adds the shifted class to the container, then waits 500ms before appending the grids to the top container:
const generateGrids = () => {
const mainBoardsContainer = document.querySelector('.gameboards');
const shipMainContainer = document.querySelector('.ship-main-container');
const transitionContainer = document.querySelector('.transition-container');
const playerBoard = document.createElement('div');
const computerBoard = document.createElement('div');
playerBoard.className = 'grid left';
computerBoard.className = 'grid right';
playerBoard.dataset.grid = false;
makeGridSquares(playerBoard, false);
makeGridSquares(computerBoard, true);
transitionContainer.classList.add('shift-down');
setTimeout(() => {
mainBoardsContainer.append(playerBoard, computerBoard);
playerBoard.classList.add('visible');
computerBoard.classList add('visible');
}, 500);
}
Here is my codepen if you'd like to see the full code: CodePen Link
英文:
I'm at a bit of a loss on my transition animation, I've replicated the problem in a codepen to share.
When I click the button, it creates two grids. Then applies a class to the bottom container to shift its Y position down the page. A setTimeout waits for this transition to end and then it appends these two grids to the main container.
But as you'll notice, after the animation has occurred it pushes the bottom container further down the page and i can't figure out why. I asked chatGPT but it wasn't able to come up with any potential solutions.
As I said, I've tried various ways to wait for the transitionend of the container, but it always seems to jump down, and i can't seem to get a seamless transition from its starting position to below the grids.
The related animation is happening on the transition-container style here:
.grid.left,
.grid.right {
display: flex;
transform: scale(0);
background: var(--main-background-clr);
border-radius: 5px;
flex-shrink: 0;
width: 50%;
height: 100%;
aspect-ratio: 1/1;
transition: 700ms ease;
}
.grid.left.visible,
.grid.right.visible {
transform: scale(1);
}
.transition-container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 20px;
width: 100%;
transition: all 0.5s ease;
}
.transition-container.shift-down {
transform: translateY(100%);
}
.ship-main-container {
display: flex;
flex-direction: column;
align-items: center;
padding-top: 10px;
gap: 20px;
width: 100%;
height: 200px;
background: var(--grid-border-clr);
}
This is the JavaScript code which generates the grid and adds the shifted class to the container, then waits 500ms before appending the grids to the top container :
const generateGrids = () => {
const mainBoardsContainer = document.querySelector('.gameboards');
const shipMainContainer = document.querySelector('.ship-main-container');
const transitionContainer = document.querySelector('.transition-container');
const playerBoard = document.createElement('div');
const computerBoard = document.createElement('div');
playerBoard.className = 'grid left';
computerBoard.className = 'grid right';
playerBoard.dataset.grid = false;
makeGridSquares(playerBoard, false);
makeGridSquares(computerBoard, true);
transitionContainer.classList.add('shift-down');
setTimeout(() => {
mainBoardsContainer.append(playerBoard, computerBoard);
playerBoard.classList.add('visible');
computerBoard.classList.add('visible');
}, 500);
}
Here is my codepen if you'd like to see the full code:
https://codepen.io/itswakana/pen/poObOYQ
答案1
得分: 1
以下是您要翻译的内容:
我可以采用以下两种方式之一来处理这个问题。而不是在过渡容器类上使用translateY属性,我会将位置设置为absolute,并使用top属性来处理过渡...还要给它设置一个高度。
.transition-container {
...你的其他CSS属性;
高度: min-content;
位置: 绝对;
顶部: 0vh;
}
.transition-container.shift-down {
顶部: 100vh;
}
这将为您提供平滑的过渡效果。这是一个快速修复方法。一个更详细的方法是将所有内容包装在一个main-container div中。
<div class="main-container">
<div class="gameboards">
</div>
<div class="transition-container">
<div class="ship-main-container"></div>
<button class="start-game">开始游戏</button>
</div>
</div>
为这个容器添加一些样式:
.main-container {
显示: 弹性;
弹性方向: 列;
高度: -webkit-fill-available; (或100%)
对齐方式: 弹性开始;
宽度: 100%;
}
现在,如果您知道游戏板和过渡容器之间的高度差,您可以将过渡容器的高度设置为所需的高度(这不是真正必要的,但我认为对于两者都设置高度是不错的),并为游戏板设置一个高度为0%,然后将一个类添加到它,该类将使高度变为所需的高度差。您只需要确保在设置游戏板的高度后正确处理时间,以便在设置高度后插入行。
请注意,您可能可以将main-container的样式直接添加到body元素上,但我更喜欢在创建布局时使用divs而不是在body元素上添加样式。希望这有所帮助。
英文:
I would go about this one of two ways. Rather than using the translateY property on the transition-container class, I would set the position to absolute and use the top property to handle the transition...also give it a height.
.transition-container {
...your other css properties;
height: min-content;
position: absolute;
top: 0vh;
}
.transition-container.shift-down {
top: 100vh;
}
This will give you that smooth transition. That's the quick fix. A more detailed approach would be to wrap everything in a main-container div.
<div class="main-container">
<div class="gameboards">
</div>
<div class="transition-container">
<div class="ship-main-container"></div>
<button class="start-game">Start Game</button>
</div>
</div>
Give that sucker some styles:
.main-container {
display: flex;
flex-direction: column;
height: -webkit-fill-available; (or 100%)
justify-content: flex-start;
width: 100%;
}
Now, if you know the split in height between gameboards and transition-container, you can set the transition-container to the desired height (not really necessary but I think its nice to have a height for both) and give the gameboards a height: 0% that you will then add a class to it that will transform the height to whatever the desired split is. You would just have to make sure you handle the timing correctly so that your rows are inserted after the height is set on gameboards.
Do note you could probably just put the main-container styles on the body element but I prefer using divs when creating my layout vs styling my body element. I hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论