【问题标题】:Grid Element expand animations using CSSGrid Element 使用 CSS 扩展动画
【发布时间】:2019-01-20 06:58:41
【问题描述】:

我有以下

setTimeout(function(){
var sections = document.querySelectorAll('section'), main = document.querySelector('.grid-container'), 
//section = sections[Math.floor(Math.random() * 3)];
section = sections[1];
section.classList.add('expand');
section.parentElement.classList.add('expand');
main.classList.add('expanding');
}, 2000);
*{
	box-sizing : border-box; 
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale; 
}

.grid-container {
  width:100%;
  height: 100vh;
  display:flex;
  border: 1px solid;
  flex-wrap: wrap;
  flex-direction: column;
}

.grid-row, .grid-container {
  
  overflow:hidden;
}

.grid-column, .grid-row {
  display: flex;
  transition: width .2s, height .2s, margin .2s, transform .2s;
}

.grid-column {
  width: inherit;
  height: inherit;
  align-items: center;
  justify-items: center;
}

.grid-column:nth-child(1) {
  background-color:green;
}

.grid-column:nth-child(2) {
  background-color:orange;
}
.grid-row:nth-child(2) .grid-column:nth-child(1) {
  background-color:violet;
}

.grid-row:nth-child(2) .grid-column:nth-child(2) {
  background-color:brown;
}

@media screen and (orientation: portrait) {
  .grid-row {
    width: 100%;
    height: 50%;
    flex-direction: column;
  }
  
  .grid-column {
    width: 100%;
    height: 50%;
  }
  
  .grid-row.expand .grid-column {
    transform: translateY(-50%);
  }
 
}

@media screen and (orientation: landscape) {
  .grid-row {
    width: 50%;
    height: 100%;
  }
  
  .grid-column {
    width: 50%;
    height: 100%;
  }
}

@media screen and (min-height: 500px) {
  .grid-row {
    height: 50%;
    width: 100%;
  }
}

.expanding .expand {
  width: 100% !important;
  height: 100% !important;
}
<main class="grid-container">
<div class="grid-row" data-index="1">
  <section class="grid-column" data-index="1">
    <article>
      <p>
      1
      </p>
    </article>
  </section>
  <section class="grid-column" data-index="2">
    <article>
      <p>
      2
      </p>
    </article>
  </section>
  </div>
  <div class="grid-row" data-index="2">
  <section class="grid-column" data-index="1">
    <article>
      <p>
      3
      </p>
    </article>
  </section>
  <section class="grid-column" data-index="2">
    <article>
      <p>
      4
      </p>
    </article>
  </section>
  </div>
</main>

我正在尝试开发网格中的“单元格”在扩展到视图的整个区域时将动画化到好像它“将”相邻元素“推”出视图的位置。

我正在尝试使用宽度、高度和变换的组合来提供适当的动画,但变换似乎给出了一些意想不到的结果...

有没有办法在不使用position:absolute 或使用尽可能少的 javascript 的情况下完成此操作??

【问题讨论】:

  • 不清楚你在问什么。是的,没有 JavaScript 也可以做到。问题是:何时?您希望它在设定的时间间隔后或在某些用户交互后发生吗?您可以在没有 JavaScript 的情况下实现这两者,但首先您必须定义需求。此外,您应该将代码放在问题内的堆栈 sn-p 中,这样即使您更改或删除小提琴,您的问题仍然相关。
  • “这些转换似乎给出了一些意想不到的结果” 是什么意思?预期的结果是什么?请具体。

标签: css css-transforms


【解决方案1】:

这是一个看似做你想做的事的例子,从头开始编码,没有任何 JavaScript。我使用了 flexbox 而不是 grid,但也可以使用 grid 来实现。我使用:hover 作为触发器:

body {
  padding: 15px;
  background-color: #999;
  margin: 0;
}

.grid>*:hover, 
.grid>*>*:hover {
  flex-grow: 3;
}

.grid>*>*:hover {
  box-shadow: 0 4px 5px -2px rgba(0, 0, 0, .2), 0 7px 10px 1px rgba(0, 0, 0, .14), 0 2px 16px 1px rgba(0, 0, 0, .12);
  z-index: 1;
  opacity: 1;
  font-size: 5rem;
}
.grid:hover>*:not(:hover) {
  flex-grow: 0;
  max-height: 0;
  overflow: hidden;
}
.grid>*,
.grid>*>* {
  transition: all .6s cubic-bezier(0.5, 0, 0.3, 1);
}

.grid {
  flex-direction: column;
  min-height: calc(100vh - 30px);
  box-sizing: border-box;
}

.grid,
.grid>* {
  display: flex;
  max-height: 100%;
}

.grid>* {
  flex-direction: row;
  margin: 0;
}

.grid>*,
.grid>*>* {
  flex-grow: 1;
}

.grid>*>* {
  margin: 0;
  z-index: 0;
  position: relative;
  background-color: #fff;
  outline: 1px solid #ddd;
  opacity: .95;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
  font-size: 3rem;
  overflow: hidden;
}
<div class="grid">
  <div>
    <div>1</div>
    <div>2</div>
  </div>
  <div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
  </div>
  <div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
  </div>
</div>

您可以使用flex-grow,更改动画或悬停元素与非悬停元素之间的比例,但为此,您需要比“似乎给出一些意想不到的结果”更好地定义要求 em>:

您显然可以将其限制为 2 + 2 个元素。我只是想指出它在结构方面是灵活的。下面是一个例子,在垂直和水平方向上都扩展为完全:

body {
  padding: 15px;
  background-color: #999;
  margin: 0;
}

.grid>*:hover, 
.grid>*>*:hover {
  flex-grow: 3;
}

.grid>*>*:hover {
  box-shadow: 0 4px 5px -2px rgba(0, 0, 0, .2), 0 7px 10px 1px rgba(0, 0, 0, .14), 0 2px 16px 1px rgba(0, 0, 0, .12);
  z-index: 1;
  opacity: 1;
  font-size: 5rem;
}
.grid:hover>*:not(:hover) {
  flex-grow: 0;
  max-height: 0;
  overflow: hidden;
}
.grid>*:hover>*:not(:hover) {
  flex-grow: 0;
  flex-basis: 0;
  overflow: hidden;
}
.grid>*,
.grid>*>* {
  transition: all .6s cubic-bezier(0.5, 0, 0.3, 1);
}

.grid {
  flex-direction: column;
  min-height: calc(100vh - 30px);
  box-sizing: border-box;
}

.grid,
.grid>* {
  display: flex;
  max-height: 100%;
}

.grid>* {
  flex-direction: row;
  margin: 0;
}

.grid>*,
.grid>*>* {
  flex-grow: 1;
}

.grid>*>* {
  margin: 0;
  z-index: 0;
  position: relative;
  background-color: #fff;
  outline: 1px solid #ddd;
  opacity: .95;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
  font-size: 3rem;
  overflow: hidden;
  max-width: 100%;
  flex-basis: 100%;
  flex-grow: 0;
}
<div class="grid">
  <div>
    <div>1</div>
    <div>2</div>
  </div>
  <div>
    <div>3</div>
    <div>4</div>
  </div>
</div>

一般来说,我会避免使用全宽/全高,因为这会使选择另一个项目变得更加困难。

【讨论】:

  • Gheorghi 这正是我正在寻找的效果,除了“悬停”元素应该扩展到视口的整个宽度和高度......将看看flex-grow定义
  • @Kendall,这有点棘手,因为您还需要定义 flex-basisflex-shrink 以便动画不会跳转。我将其添加到答案中。通常应避免扩展完整,除非您提供对应在其他地方扩展的内容的控制,因为这会使选择另一个项目变得困难,尤其是当您每行有超过 2 个项目或超过 2 行时,但这更多的是 UX问题。
  • 我明白了。为什么是flex-basis 而不是flex-shrink
  • 请参阅this example 了解用户体验问题。参考flex-basis,我认为没有它也可以实现,但由于某种原因,动画在开始扩展时会从 0 跳转到内容宽度。没有花太多时间在它上面,因为它在定义flex-basis 之后起作用。我天生就是懒惰的:D
猜你喜欢
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 2016-07-31
  • 1970-01-01
相关资源
最近更新 更多