如何使用JavaScript在动画中换行?

时间:2018-12-05 04:17:03

标签: javascript html css

我正在尝试实现类型编写器动画,以便在我之间使用

function splitLetters(word) {
  var content = word.innerHTML;
  word.innerHTML ='';
  var letters = [];
  for (var i = 0; i < content.length; i++) {
    var letter = document.createElement('span');
    letter.className = 'letter';
    var char = content.charAt(i);
    letter.innerHTML = char===" "?"&nbsp;":char;
    word.appendChild(letter);
    letters.push(letter);
  }

  wordArray.push(letters);
}
  

letter.innerHTML = char ===“”?“ :: char;

以同样的方式,我想给换行符而不是&nbsp,以及可能的解决方法?

让我们假设我想显示

  

优秀的Web开发人员

现在我希望它以这种格式显示

great web
developer

伟大与网络之间的空间以及开发人员之前的换行符。

任何帮助将不胜感激。 另外,我也不希望将单词像精彩的单词一样分成两个单词。

var words = document.getElementsByClassName('word');
var wordArray = [];
var currentWord = 0;


for (var i = 0; i < words.length; i++) {
  splitLetters(words[i]);
}

function changeWord() {
  var cw = wordArray[currentWord];
  var nw = currentWord == words.length-1 ? wordArray[0] : wordArray[currentWord+1];
  for (var i = 0; i < cw.length; i++) {
    animateLetterOut(cw, i);
  }

  for (var i = 0; i < nw.length; i++) {
    nw[i].className = 'letter behind';
    nw[0].parentElement.style.opacity = 1;
    animateLetterIn(nw, i);
  }

  currentWord = (currentWord == wordArray.length-1) ? 0 : currentWord+1;
}

function animateLetterOut(cw, i) {
  setTimeout(function() {
		cw[i].className = 'letter out';
  }, i*80);
}

function animateLetterIn(nw, i) {
  setTimeout(function() {
		nw[i].className = 'letter in';
  }, 340+(i*80));
}

function splitLetters(word) {
  var content = word.innerHTML;
  word.innerHTML ='';
  var letters = [];
  for (var i = 0; i < content.length; i++) {
    var letter = document.createElement('span');
    letter.className = 'letter';
    var char = content.charAt(i);
    letter.innerHTML = char===" "?"&nbsp;":char;
    word.appendChild(letter);
    letters.push(letter);
  }

  wordArray.push(letters);
}

changeWord();
setInterval(changeWord, 4000);
:root {
  --mainColor: #fff;
}

body{
  background-color: #252627;
  margin: 0px;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.page{
  width: 100%;
  height: 100%;
  position: absolute;
}

.bglogo{
  position: absolute;
  top:20%;
  right: 15vh;
  height: 65vh;
  width: 65vh;
  z-index: 0;
}


path {
  fill: #252627;
  stroke: #00ffdc;
  animation: my_animation 2s linear forwards, filling .2s linear 2s forwards;
  stroke-dasharray: 400; /* need to ... */
  stroke-dashoffset: 400; /* ... match */
}

@keyframes my_animation {
  to {stroke-dashoffset: 0}
}

@keyframes filling {
  to {fill: #ff0046}
}

@import url(https://fonts.googleapis.com/css?family=Open+Sans:600);

body {
  font-family: 'Open Sans', sans-serif;
  font-weight: 600;
  font-size: 40px;
}

.text {
  position: absolute;
  left: 10%;
  top: 50%;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
  width: 40%;
  max-height: 90%;
}

p {
  display: inline-block;
  vertical-align: top;
  margin: 0;
  color: white;
  font-size: 64px;
}


.word {
  position: absolute;
  width: 100%;
  opacity: 0;
  white-space: nowrap;
}

.letter {
  display: inline-block;
  position: relative;
  float: left;
  transform: translateZ(25px);
  transform-origin: 50% 50% 25px;
}

.letter.out {
  transform: rotateX(90deg);
  transition: transform 0.32s cubic-bezier(0.55, 0.055, 0.675, 0.19);
}

.letter.behind {
  transform: rotateX(-90deg);
}

.letter.in {
  transform: rotateX(0deg);
  transition: transform 0.38s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.wisteria {
  color: #8e44ad;
}

.belize {
  color: #2980b9;
}

.pomegranate {
  color: #c0392b;
}

.green {
  color: #16a085;
}

.midnight {
  color: #2c3e50;
}


p {
  color: var(--mainColor);
  text-decoration: none;
}

p:hover {
  color: transparent;
  -webkit-text-stroke: 2px var(--mainColor);
}

@supports not(-webkit-text-stroke: 2px red) {
  p:hover {
    text-shadow:
      3px 3px 0 var(--mainColor),
      -1px -1px 0 var(--mainColor),
      1px -1px 0 var(--mainColor),
      -1px 1px 0 var(--mainColor),
      1px 1px 0 var(--mainColor);
  }
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Vivank Sharma</title>
    <link rel="stylesheet" href="css/master.css">
    <link rel="stylesheet" href="css/master_responsive.css">
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

  </head>
  <body>


    <div class="page">
      <div class="bglogo">
      <?xml version="1.0" encoding="iso-8859-1"?>
      <!-- Generator: Adobe Illustrator 18.1.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 89.999 89.999" style="enable-background:new 0 0 89.999 89.999;" xml:space="preserve">
        <g>
          <path d="M89.551,24.201c-5.029,28.863-33.127,53.301-41.577,58.883c-8.454,5.582-16.163-2.236-18.96-8.148   c-3.201-6.738-12.793-43.285-15.307-46.311C11.195,25.596,3.656,31.65,3.656,31.65L0,26.768c0,0,15.307-18.623,26.957-20.951   c12.35-2.469,12.332,19.318,15.301,31.416c2.873,11.701,4.807,18.398,7.312,18.398c2.514,0,7.311-6.525,12.562-16.531   c5.264-10.016-0.225-18.857-10.505-12.568C55.738,1.395,94.578-4.65,89.551,24.201z" fill="#ff0043"/>
        </g>
      </svg>
    </div>

    <div class="text">

      
      
      <p>
        <span class="word wisteria">wonderful developer</span>
        <span class="word belize">a great web devloper</span>
        
      </p>
      <p id="demo2"><p>


    </div>


  </body>
  <script type="text/javascript" src="JS/typewriter.js"></script>
</html>

1 个答案:

答案 0 :(得分:1)

一种简单的方法是插入<br>标记而不是span。您可以对所有空格进行如下操作:

var words = document.getElementsByClassName('word');
var wordArray = [];
var currentWord = 0;


for (var i = 0; i < words.length; i++) {
  splitLetters(words[i]);
}

function changeWord() {
  var cw = wordArray[currentWord];
  var nw = currentWord == words.length-1 ? wordArray[0] : wordArray[currentWord+1];
  for (var i = 0; i < cw.length; i++) {
    animateLetterOut(cw, i);
  }

  for (var i = 0; i < nw.length; i++) {
    nw[i].className = 'letter behind';
    nw[0].parentElement.style.opacity = 1;
    animateLetterIn(nw, i);
  }

  currentWord = (currentWord == wordArray.length-1) ? 0 : currentWord+1;
}

function animateLetterOut(cw, i) {
  setTimeout(function() {
		cw[i].className = 'letter out';
  }, i*80);
}

function animateLetterIn(nw, i) {
  setTimeout(function() {
		nw[i].className = 'letter in';
  }, 340+(i*80));
}

function splitLetters(word) {
  var content = word.innerHTML;
  word.innerHTML ='';
  var letters = [];
  for (var i = 0; i < content.length; i++) {
    var letter = document.createElement('span');
    letter.className = 'letter';
    var char = content.charAt(i);
    if(char === " ")
        letter = document.createElement('br');
    else
        letter.innerHTML = char;
    word.appendChild(letter);
    letters.push(letter);
  }

  wordArray.push(letters);
}

changeWord();
setInterval(changeWord, 4000);
:root {
  --mainColor: #fff;
}

body{
  background-color: #252627;
  margin: 0px;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.page{
  width: 100%;
  height: 100%;
  position: absolute;
}

.bglogo{
  position: absolute;
  top:20%;
  right: 15vh;
  height: 65vh;
  width: 65vh;
  z-index: 0;
}


path {
  fill: #252627;
  stroke: #00ffdc;
  animation: my_animation 2s linear forwards, filling .2s linear 2s forwards;
  stroke-dasharray: 400; /* need to ... */
  stroke-dashoffset: 400; /* ... match */
}

@keyframes my_animation {
  to {stroke-dashoffset: 0}
}

@keyframes filling {
  to {fill: #ff0046}
}

@import url(https://fonts.googleapis.com/css?family=Open+Sans:600);

body {
  font-family: 'Open Sans', sans-serif;
  font-weight: 600;
  font-size: 40px;
}

.text {
  position: absolute;
  left: 10%;
  top: 50%;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
  width: 40%;
  max-height: 90%;
}

p {
  display: inline-block;
  vertical-align: top;
  margin: 0;
  color: white;
  font-size: 64px;
}


.word {
  position: absolute;
  width: 100%;
  opacity: 0;
  white-space: nowrap;
}


.letter:not(br) {
  display: inline-block;
  position: relative;
  float: left;
  transform: translateZ(25px);
  transform-origin: 50% 50% 25px;
}
br {
 clear:left;
}
.letter.out:not(br) {
  transform: rotateX(90deg);
  transition: transform 0.32s cubic-bezier(0.55, 0.055, 0.675, 0.19);
}

.letter.behind:not(br) {
  transform: rotateX(-90deg);
}

.letter.in:not(br) {
  transform: rotateX(0deg);
  transition: transform 0.38s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.wisteria {
  color: #8e44ad;
}

.belize {
  color: #2980b9;
}

.pomegranate {
  color: #c0392b;
}

.green {
  color: #16a085;
}

.midnight {
  color: #2c3e50;
}


p {
  color: var(--mainColor);
  text-decoration: none;
}

p:hover {
  color: transparent;
  -webkit-text-stroke: 2px var(--mainColor);
}

@supports not(-webkit-text-stroke: 2px red) {
  p:hover {
    text-shadow:
      3px 3px 0 var(--mainColor),
      -1px -1px 0 var(--mainColor),
      1px -1px 0 var(--mainColor),
      -1px 1px 0 var(--mainColor),
      1px 1px 0 var(--mainColor);
  }
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Vivank Sharma</title>
    <link rel="stylesheet" href="css/master.css">
    <link rel="stylesheet" href="css/master_responsive.css">
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

  </head>
  <body>


    <div class="page">
      <div class="bglogo">
      <?xml version="1.0" encoding="iso-8859-1"?>
      <!-- Generator: Adobe Illustrator 18.1.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 89.999 89.999" style="enable-background:new 0 0 89.999 89.999;" xml:space="preserve">
        <g>
          <path d="M89.551,24.201c-5.029,28.863-33.127,53.301-41.577,58.883c-8.454,5.582-16.163-2.236-18.96-8.148   c-3.201-6.738-12.793-43.285-15.307-46.311C11.195,25.596,3.656,31.65,3.656,31.65L0,26.768c0,0,15.307-18.623,26.957-20.951   c12.35-2.469,12.332,19.318,15.301,31.416c2.873,11.701,4.807,18.398,7.312,18.398c2.514,0,7.311-6.525,12.562-16.531   c5.264-10.016-0.225-18.857-10.505-12.568C55.738,1.395,94.578-4.65,89.551,24.201z" fill="#ff0043"/>
        </g>
      </svg>
    </div>

    <div class="text">

      
      
      <p>
        <span class="word wisteria">wonderful developer</span>
        <span class="word belize">a great web devloper</span>
        
      </p>
      <p id="demo2"><p>


    </div>


  </body>
  <script type="text/javascript" src="JS/typewriter.js"></script>
</html>

或添加基于空间数或其他条件的条件:

var words = document.getElementsByClassName('word');
var wordArray = [];
var currentWord = 0;
var spaces = 2;


for (var i = 0; i < words.length; i++) {
  splitLetters(words[i]);
}

function changeWord() {
  var cw = wordArray[currentWord];
  var nw = currentWord == words.length-1 ? wordArray[0] : wordArray[currentWord+1];
  for (var i = 0; i < cw.length; i++) {
    animateLetterOut(cw, i);
  }

  for (var i = 0; i < nw.length; i++) {
    nw[i].className = 'letter behind';
    nw[0].parentElement.style.opacity = 1;
    animateLetterIn(nw, i);
  }

  currentWord = (currentWord == wordArray.length-1) ? 0 : currentWord+1;
}

function animateLetterOut(cw, i) {
  setTimeout(function() {
		cw[i].className = 'letter out';
  }, i*80);
}

function animateLetterIn(nw, i) {
  setTimeout(function() {
		nw[i].className = 'letter in';
  }, 340+(i*80));
}


function splitLetters(word) {
  var content = word.innerHTML;
  word.innerHTML ='';
  var letters = [];
  var space = 0;
  for (var i = 0; i < content.length; i++) {
    var letter = document.createElement('span');
    letter.className = 'letter';
    var char = content.charAt(i);
    if(char === " ") {
        if(space == spaces)
            letter = document.createElement('br');
        else
            letter.innerHTML = "&nbsp;";
        space++;       
    }
    else
        letter.innerHTML = char;
    word.appendChild(letter);
    letters.push(letter);
  }

  wordArray.push(letters);
}

changeWord();
setInterval(changeWord, 4000);
:root {
  --mainColor: #fff;
}

body{
  background-color: #252627;
  margin: 0px;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.page{
  width: 100%;
  height: 100%;
  position: absolute;
}

.bglogo{
  position: absolute;
  top:20%;
  right: 15vh;
  height: 65vh;
  width: 65vh;
  z-index: 0;
}


path {
  fill: #252627;
  stroke: #00ffdc;
  animation: my_animation 2s linear forwards, filling .2s linear 2s forwards;
  stroke-dasharray: 400; /* need to ... */
  stroke-dashoffset: 400; /* ... match */
}

@keyframes my_animation {
  to {stroke-dashoffset: 0}
}

@keyframes filling {
  to {fill: #ff0046}
}

@import url(https://fonts.googleapis.com/css?family=Open+Sans:600);

body {
  font-family: 'Open Sans', sans-serif;
  font-weight: 600;
  font-size: 40px;
}

.text {
  position: absolute;
  left: 10%;
  top: 50%;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
  width: 40%;
  max-height: 90%;
}

p {
  display: inline-block;
  vertical-align: top;
  margin: 0;
  color: white;
  font-size: 64px;
}


.word {
  position: absolute;
  width: 100%;
  opacity: 0;
  white-space: nowrap;
}

.letter:not(br) {
  display: inline-block;
  position: relative;
  float: left;
  transform: translateZ(25px);
  transform-origin: 50% 50% 25px;
}

br {
 clear:left;
}

.letter.out:not(br) {
  transform: rotateX(90deg);
  transition: transform 0.32s cubic-bezier(0.55, 0.055, 0.675, 0.19);
}

.letter.behind:not(br) {
  transform: rotateX(-90deg);
}

.letter.in:not(br) {
  transform: rotateX(0deg);
  transition: transform 0.38s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.wisteria {
  color: #8e44ad;
}

.belize {
  color: #2980b9;
}

.pomegranate {
  color: #c0392b;
}

.green {
  color: #16a085;
}

.midnight {
  color: #2c3e50;
}


p {
  color: var(--mainColor);
  text-decoration: none;
}

p:hover {
  color: transparent;
  -webkit-text-stroke: 2px var(--mainColor);
}

@supports not(-webkit-text-stroke: 2px red) {
  p:hover {
    text-shadow:
      3px 3px 0 var(--mainColor),
      -1px -1px 0 var(--mainColor),
      1px -1px 0 var(--mainColor),
      -1px 1px 0 var(--mainColor),
      1px 1px 0 var(--mainColor);
  }
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Vivank Sharma</title>
    <link rel="stylesheet" href="css/master.css">
    <link rel="stylesheet" href="css/master_responsive.css">
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

  </head>
  <body>


    <div class="page">
      <div class="bglogo">
      <?xml version="1.0" encoding="iso-8859-1"?>
      <!-- Generator: Adobe Illustrator 18.1.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 89.999 89.999" style="enable-background:new 0 0 89.999 89.999;" xml:space="preserve">
        <g>
          <path d="M89.551,24.201c-5.029,28.863-33.127,53.301-41.577,58.883c-8.454,5.582-16.163-2.236-18.96-8.148   c-3.201-6.738-12.793-43.285-15.307-46.311C11.195,25.596,3.656,31.65,3.656,31.65L0,26.768c0,0,15.307-18.623,26.957-20.951   c12.35-2.469,12.332,19.318,15.301,31.416c2.873,11.701,4.807,18.398,7.312,18.398c2.514,0,7.311-6.525,12.562-16.531   c5.264-10.016-0.225-18.857-10.505-12.568C55.738,1.395,94.578-4.65,89.551,24.201z" fill="#ff0043"/>
        </g>
      </svg>
    </div>

    <div class="text">

      
      
      <p>
        <span class="word wisteria">wonderful developer</span>
        <span class="word belize">a great web devloper</span>
        
      </p>
      <p id="demo2"><p>


    </div>


  </body>
  <script type="text/javascript" src="JS/typewriter.js"></script>
</html>

我还调整了一些CSS,以使br不会受到其他类的影响