CSS анимация для расширения div от центра кнаружи - PullRequest
0 голосов
/ 05 декабря 2018

Я пытаюсь сделать следующее расширение наружу из центра (почти как при открытии штор).

Это должно выглядеть примерно так, но вместо - это пустое пространство.

  1. -------------
  2. ------t------
  3. -----sti-----
  4. ----estin----
  5. ---Testing---

.animation{
  width: 100%;
  text-align: center;
  font-size: 50px;
  padding: 25px;
  background: black;
  color: white;
}
<div class="animation">
Testing
</div>

Я также хотел бы сделать это без использования JavaScript, если это возможно.

Ответы [ 3 ]

0 голосов
/ 05 декабря 2018

Вы можете попробовать перевести width и использовать flex для выравнивания текста

.animation {
  width: 100%;
  font-size: 50px;
  padding: 25px 0;
  background: black;
  color: white;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  animation: grow 2s;
}

@keyframes grow {
  0% {
    width: 0;
  }
  100% {
    width: 100%;
  }
}
<div class="animation">
Testing
</div>
0 голосов
/ 05 декабря 2018

здесь проще, без clip-path и без флексбокса

.animation{
  width: 0%;
  margin:auto;
  display:flex;
  justify-content: center;
  white-space:nowrap;
  font-size: 50px;
  padding: 25px 0;
  background: black;
  color: white;
  overflow:hidden;
  transition:0.5s all;
}
body:hover .animation{
  width:100%;
}
<div class="animation">
Testing
</div>

Вы также можете играть с псевдоэлементом и фоном, если вам не нужна прозрачность:

.animation{
  width: 100%;
  text-align: center;
  font-size: 50px;
  padding: 25px 0;
  background: black;
  color: white;
  position:relative;
}
.animation:before{
  content:"";
  position:absolute;
  top:0;
  right:0;
  left:0;
  bottom:0;
  background:
    linear-gradient(#fff,#fff)left/50.5% 100%,
    linear-gradient(#fff,#fff)right/50.5% 100%;
  background-repeat:no-repeat;
  transition:0.5s all;
}
.animation:hover::before {
  background-size:0% 100%;
}
<div class="animation">
Testing
</div>

Другая идея с использованием box-shadow:

.animation{
  width: 100%;
  text-align: center;
  font-size: 50px;
  padding: 25px 0;
  background: black;
  color: white;
  position:relative;
  box-shadow:
    50vw 0 0 #fff inset,
    -50vw 0 0 #fff inset;
  transition:0.5s all;
}
.animation:hover {
  box-shadow:
    0 0 0 #fff inset,
    0 0 0 #fff inset;
}
<div class="animation">
Testing
</div>
0 голосов
/ 05 декабря 2018

Используйте clip-path и CSS-анимацию.

Примечание : в Safari и других браузерах необходим префикс -webkit


Первый ключевой кадр имеет 4 точки в центре на оси X, нов их последнем (максимальном) месте на оси Y.Это позволяет нам анимировать только ось X.

Второй ключевой кадр перемещает 4 точки наружу в их максимальные позиции вдоль оси X, при этом часть Y остается неизменной.

Немного запутанноно смотри ниже:

.animation{
  width: 100%;
  text-align: center;
  font-size: 50px;
  padding: 25px;
  background: black;
  color: white;
  animation: expand_center 5000ms;
  animation-fill-mode: forwards;
}
@keyframes expand_center {
  0% {
  clip-path: polygon(50% 100%,50% 0,50% 0,50% 100%);
    -webkit-clip-path: polygon(50% 100%,50% 0,50% 0,50% 100%);
 }
  100%{
  clip-path: polygon(0 100%, 0 0, 100% 0, 100% 100%);
    -webkit-clip-path: polygon(0 100%, 0 0, 100% 0, 100% 100%);
  }
}
<div class="animation">
Testing
</div>

Благодарим @KendallFrey за идею пути клипа.

...