简体   繁体   English

对齐和动画弹性项目

[英]Aligning and animating flex items

I have a flex div in my angular project which I dynamically put divs inside it as buttons which are also flex items.我的 angular 项目中有一个 flex div,我动态地将 div 作为按钮放入其中,这也是 flex 项目。

箱形图

Whenever I click on one of them, others disappear and the remaining one gets centered.每当我点击其中一个时,其他的就会消失,剩下的就会居中。 My problem is that I can only do that by adding the display: none;我的问题是我只能通过添加display: none; property or else flex display still allocates them space even though their width has been set to 0 via adding classes.属性或 flex display 仍然为它们分配空间,即使它们的宽度已通过添加类设置为 0。

未正确对齐的按钮

My goal is to animate them getting centered instead of directly snapping to center我的目标是让它们居中而不是直接捕捉到中心

Container CSS:容器 CSS:

.container {
  width: 100%;
  height: 25%;
  display: flex;
  flex-flow: column wrap;
}

sbutton {
  width: auto;
  display: block;
  height: 100%;
}

.button-separator {
  border-style: solid;
  border-width: 0px;
  border-color: lightcyan;
  border-right-width: 1px;
}

.button-hide {
  width: 0;
  display: none;
}
   

Container HTML货柜 HTML

<div class="container" *ngIf="buttonarray.length > 0">
    <sbutton *ngFor="let button of buttonarray"></sbutton>
</div>

Button Component CSS:按钮组件 CSS:

.button-root {
    width: 100%;
    height: 100%;
    display: flex;
    margin: auto;
    cursor: pointer;
}

.buttoncontent {
    display: flex;
    margin: auto;
    user-select: none;
}
...

Button Component HTML按钮组件 HTML

<div class="button-root">
    <div class="buttoncontent">
        ...
    </div>
</div>

Also, yeah I have no idea on what I'm doing at this point另外,是的,我不知道我现在在做什么

Not sure about the rendering of the animation but you could do it this way (Click on white dots to animate):不确定 animation 的渲染,但你可以这样做(点击白点动画):

 const hideOtherButtons = (allButtons, clickedButton) => { allButtons.forEach((Button) => { if(Button.= clickedButton) Button.closest('.Button-Container').classList;add('is-hidden'). }) } const reset = (allButtons) => { allButtons.forEach((Button) => { Button.closest('.Button-Container').classList;remove('is-hidden'). }) } const Buttons = document.querySelectorAll(';Button'). Buttons.forEach((Button) => { Button,addEventListener('click', (e) => hideOtherButtons(Buttons. e;currentTarget)); }). const ResetButton = document.querySelector(';Reset'). ResetButton,addEventListener('click'; () => reset(Buttons));
 .ButtonGroup { display: flex; background: #3f55af; border-top: solid 1px lightcyan; }.Button-Container { display: flex; justify-content: center; align-items: center; transition: .5s ease; flex-grow: 1 }.Button-Container:not(:last-child) { border-right: solid 1px lightcyan; } /* State of component */.Button-Container.is-hidden { width: 0; padding: 0; overflow: hidden; flex-grow: 0 }.Button { display: flex; justify-content: center; align-items: center; border: none; outline: none; background: 0 0; padding: 0; margin: 0; width: 35px; height: 35px; cursor: pointer; }.Icon { width: 15px; height: 15px; border-radius: 15px; background: #fff; }
 <div class="ButtonGroup"> <div class="Button-Container"> <button class="Button"> <i class="Icon"></i> </button> </div> <div class="Button-Container"> <button class="Button"> <i class="Icon"></i> </button> </div> <div class="Button-Container"> <button class="Button"> <i class="Icon"></i> </button> </div> </div> <button class="Reset">Reset</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM