为什么我的容器在添加网格时过渡不平滑?

huangapple go评论68阅读模式
英文:

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.

       &lt;div class=&quot;main-container&quot;&gt;
&lt;div class=&quot;gameboards&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;transition-container&quot;&gt;
&lt;div class=&quot;ship-main-container&quot;&gt;&lt;/div&gt;
&lt;button class=&quot;start-game&quot;&gt;Start Game&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;

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.

huangapple
  • 本文由 发表于 2023年2月24日 10:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定