文本完成后如何停止光标

时间:2018-07-24 09:15:36

标签: 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>

2 个答案:

答案 0 :(得分:2)

这是因为.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>

答案 1 :(得分:0)

您可以通过在h1内添加一个内联元素并利用after伪元素属性来执行以下操作。 希望能帮助到你。

.typewriter h1{
position: relative;
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%}
}
span:after{
  content : '-';
  animation:blinking .75s step-end infinite;
  color  :transparent;
  display : inline-block;
  width  :5px;
}
@keyframes blink-caret{
from, to {border-color: transparent}
50% {border-color: white}
}
@keyframes blinking{
from, to {background: transparent;}
50% {background: black;}
}
<div class="typewriter">
    <h1>Welcome to my website <span></span></h1>
</div>

相关问题