css - 如何在文本结束后停止光标

标签 css animation typing

我的网页有打字效果,但有点问题。文本输入完成后,闪烁的光标会一直移动到行尾。我如何让它留在最后一个字母之后?

这是基本的,但也许我需要添加更多内容或修改它?

.typewriter h1{
  position: relative;
  top: 7em;
  width: fit-content;
  overflow: hidden;
  border-right: .15em solid white;
  white-space: nowrap;
  margin: 0 auto;
  letter-spacing: .15em;
  animation: typing 3.5s steps(100, end),
            blink-caret .75s step-end infinite;
}

@keyframes typing{
  from {width: 0}
  to {width: 100%}
}

@keyframes blink-caret{
  from, to {border-color: transparent}
  50% {border-color: white}
}
<div class="typewriter">
    <h1>Welcome to my website</h1>
</div>

最佳答案

这是因为您的.typewriter h1 的宽度设置为100%。

使用 max-width 并将 width 设置为 auto 并将 display 设置为 inline-block

.typewriter h1{
  width: auto;
  display: inline-block;
  overflow: hidden;
  border-right: .15em solid white;
  white-space: nowrap;
  margin: 0 auto;
  letter-spacing: .15em;
  animation: typing 3.5s steps(100, end),
			 blink-caret .75s step-end infinite;
}

@keyframes typing{
  from {max-width: 0}
  to {max-width: 100%}
}

@keyframes blink-caret{
  from, to {border-color: transparent}
  50% {border-color: black}
}
<div class="typewriter">
    <h1>Welcome to my website</h1>
</div>

关于css - 如何在文本结束后停止光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51494890/

相关文章:

java - Scala 想告诉我什么,我该如何解决? [需要 : java. util.List[?0] 类型 ?0]

language-agnostic - 弱类型不再需要了吗?

html - 悬停元素,在父元素 sibling child 上显示悬停效果

AngularJS:动画对象从 DIV A 到 DIV B 直线

css - 添加两个 CSS3 动画?

预分配列表时的 Python 类型转换

css - 是否有 CSS 父级选择器?

css - 如何将转换后的元素保持在另一个元素后面?

CSS:我可以通过 margin: 0 0 0 auto 右对齐一个元素吗?

Android:滚动动画?