Gibt es eine Möglichkeit, Buchstaben langsam per CSS zu Text hinzuzufügen? [closed]

Lesezeit: 6 Minuten

Benutzer-Avatar
amitava mozumder

Ich möchte ein Wort anzeigen (angenommen, langsam), gibt es eine Möglichkeit, zuerst “langsam” anzuzeigen und dann durch CSS-Animation ein paar O’s in der Mitte hinzuzufügen, wodurch es von langsam zu langsam wird.

Ich verwende das neueste Chrome, daher sind alle CSS3/HTML5-Funktionen willkommen.

Sie können die maximale Breite einer Spanne wie unten animieren.

.slow {
  display: inline-block;
  vertical-align: bottom;
  max-width: 0.5rem;
  overflow: hidden;
  animation: slow 2s ease forwards;
}

@keyframes slow {
  from {
    max-width: 0.5rem;
  }
  to {
    max-width: 3rem;
  }
}
<span>Sl</span><span class="slow">oooooo</span><span>w</span>

  • Bin ich der einzige, der nur die Hälfte sieht? o Sobald die Animation fertig ist? Safari 12 / MacOS Mojave … – Jup, es funktioniert gut auf Chrome, aber aus irgendeinem seltsamen Grund nicht auf Safari ….

    – etwas hier

    23. Oktober 2018 um 7:28 Uhr


  • @somethinghere Es wird noch seltsamer: Auf iOS (neuesten) funktionierte es beim ersten Ausführen des Snippets einwandfrei, aber danach wurde jedes Mal, wenn ich auf die Schaltfläche „Run Snippet“ drückte, nur noch ein halbes o angezeigt.

    – 11684

    23. Oktober 2018 um 11:53 Uhr

  • Das ist, weil 3rem ist der falsche Weg, um die Breite des Textes zu berechnen. Die tatsächliche Breite hängt von der Schriftart ab. Auf meinem Computer verwendet Chrome Times New Roman und funktioniert gut, aber Firefox verwendet Liberation Sans, was fehlschlägt.

    – isanae

    23. Oktober 2018 um 13:38 Uhr

  • @isanae Ich glaube nicht, dass es das ist – Safari verwendet Times New Roman und die 3rem bezieht sich eigentlich auf die :root Größe, die (unverändert) etwa 16 Pixel (rem hat nichts mit den Größen in der Schriftart zu tun, es ist nur relativ zu einer Pixelgröße, die entweder an der Wurzel (rem) oder beim ersten übergeordneten Element, das es definiert (em)). Versteh mich nicht falsch, das könnte es sein, aber es ist trotzdem seltsam.

    – etwas hier

    23. Oktober 2018 um 14:04 Uhr


  • @somethinghere Die Schriftgröße kann in den Browsereinstellungen festgelegt werden. Wenn es 16px drin ist <html>dann 3rem bedeutet 3*16, also 48 Pixel. Sechs o‘s in Times 16px ist in der Tat genau 48 Pixel, weshalb es hier meiner Meinung nach verwendet wird, da Times oft die Standardschriftart ist, wo keine angegeben ist (und Stack-Snippets-Iframes im Grunde kein CSS haben). Wenn Ihre Schriftart nicht Times ist, ist die Breite möglicherweise falsch. Befreiung bei 16px benötigt zum Beispiel 54 Pixel für sechs o‘s.

    – isanae

    23. Oktober 2018 um 14:16 Uhr


Benutzer-Avatar
etwas hier

Sie könnten alle zusätzlichen hinzufügen oist wie <span> Elemente und animieren Sie sie dann alle nacheinander mit :nth-child um sie einzeln auszuwählen:

html, body {
  height: 100%;
}
body {
  display: flex;
}
h1 {
  font-size: 10vw;
  margin: auto;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
h1 span {
  max-width: 0;
  overflow: hidden;
  display: inline-block;
  vertical-align: bottom;
}
@keyframes in {
  from { max-width: 0; opacity: 0; }
  25% { max-width: 1em; opacity: 0; }
  to { max-width: 1em; opacity: 1; }
}
h1 span {
  animation: in 1s;
  animation-fill-mode: forwards;
}
h1 span:nth-child(1){ animation-delay: 0s; }
h1 span:nth-child(2){ animation-delay: 1s; }
h1 span:nth-child(3){ animation-delay: 2s; }
h1 span:nth-child(4){ animation-delay: 3s; }
h1 span:nth-child(5){ animation-delay: 4s; }
h1 span:nth-child(6){ animation-delay: 5s; }
h1 span:nth-child(7){ animation-delay: 6s; }
h1 span:nth-child(8){ animation-delay: 7s; }
h1 span:nth-child(9){ animation-delay: 8s; }
<h1>Slo<span>o</span><span>o</span><span>o</span><span>o</span><span>o</span><span>o</span><span>o</span><span>o</span><span>o</span>w</h1>

  • Unter Windows und Firefox sind die zusätzlichen “o” nach oben versetzt, wobei ihre Spitze am oberen Ende des S und l und ihre Grundlinie ungefähr in der Mitte des ersten o liegt.

    – Tom

    23. Oktober 2018 um 10:00 Uhr

  • @tom Muss es Firefox geben, wo inline-block bedeutet ‘lasst uns das nicht an der Linie ausrichten’; nichts das vertical-align: bottom behebt nicht, aber warum FF sich besonders verhalten muss, weiß Gott.

    – etwas hier

    23. Oktober 2018 um 11:09 Uhr


  • jetzt wird es richtig angezeigt.

    – Tom

    23. Oktober 2018 um 11:38 Uhr

Nur zum Spaß, hier ist eine Lösung in echtem reinem CSS (dh nur ein einziges HTML-Element erforderlich), mit der content CSS-Eigenschaft:

.expanding-slow::before {
  content: "Slo";
  animation: expand-slow linear 3s both;
}
.expanding-slow::after { content: "w"; }
@keyframes expand-slow {
  0% { content: "Slo"; }
  20% { content: "Sloo"; }
  40% { content: "Slooo"; }
  60% { content: "Sloooo"; }
  80% { content: "Slooooo"; }
  100% { content: "Sloooooo"; }
}

.expanding-slow--smooth::before {
  display: inline-block;
  content: "Sloooooo";
  max-width: 3ch;
  overflow: hidden;
  vertical-align: text-top;
  animation: expand-slow--smooth linear 3s both;
}
.expanding-slow--smooth::after { content: "w"; }
@keyframes expand-slow--smooth {
  0% { max-width: 3ch; }
  100% { max-width: 8ch; }
}
Using <code>content</code>:
<p class="expanding-slow"></p>

Using <code>max-width</code>:
<p class="expanding-slow--smooth"></p>

  • Verwenden content funktioniert nicht in Safari 12/MacOS Mojave. Das max-width man tut und seine clever.

    – etwas hier

    24. Oktober 2018 um 7:37 Uhr

Hier ist eine überarbeitete Version der Antwort von @DarioSanchezMartinez , die Ihrer Spezifikation etwas näher kommt.

/* Taken from http://animista.net/play/entrances/fade-in */


#animate-1 {
	-webkit-animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
	        animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
          
}

#animate-2 {
	-webkit-animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
	        animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
          animation-delay: 1s;
}

#animate-3 {
	-webkit-animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
	        animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
          animation-delay: 2s;
}

#animate-4 {
	-webkit-animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
	        animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
          animation-delay: 3s;
}
@-webkit-keyframes fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
@keyframes fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
SL<span id="animate-1">o</span><span id="animate-2">o</span><span id="animate-3">o</span><span id="animate-4">o</span>W

Ja!

/* Taken from http://animista.net/play/entrances/fade-in */


#animate-1 {
	-webkit-animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
	        animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
          
}

#animate-2 {
	-webkit-animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
	        animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
          animation-delay: 1s;
}

#animate-3 {
	-webkit-animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
	        animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
          animation-delay: 2s;
}

#animate-4 {
	-webkit-animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
	        animation: fade-in 1.2s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
          animation-delay: 3s;
}
@-webkit-keyframes fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
@keyframes fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<h1 id="animate-1">S</h1>
<h1 id="animate-2">L</h1>
<h1 id="animate-3">O</h1>
<h1 id="animate-4">W</h1>

  • Dies druckt nur den Buchstaben einzeln. nicht das was ich suche

    – Amitava-Mozumder

    23. Oktober 2018 um 7:39 Uhr

  • Diese Antwort könnte behoben werden: Ändern Sie einfach alle Zeichen in “o”, schreiben Sie sl und w um ihn herum und ändern Sie H1 in SPAN’s.

    – Martijn

    24. Oktober 2018 um 9:42 Uhr

  • Dies druckt nur den Buchstaben einzeln. nicht das was ich suche

    – Amitava-Mozumder

    23. Oktober 2018 um 7:39 Uhr

  • Diese Antwort könnte behoben werden: Ändern Sie einfach alle Zeichen in “o”, schreiben Sie sl und w um ihn herum und ändern Sie H1 in SPAN’s.

    – Martijn

    24. Oktober 2018 um 9:42 Uhr

1233920cookie-checkGibt es eine Möglichkeit, Buchstaben langsam per CSS zu Text hinzuzufügen? [closed]

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy