【问题标题】:Avoid words split避免单词分裂
【发布时间】:2016-01-23 16:18:30
【问题描述】:

我正在尝试使用一些淡入/淡出效果逐个字母地重复显示一些句子。但是,当尝试这样做时,似乎单词在中间中断,如下所示。如何避免断字?

var quotes = document.getElementsByClassName('quote');
var quoteArray = [];
var currentQuote = 0;
quotes[currentQuote].style.opacity = 0;
for (var i = 0; i < quotes.length; i++) {
  splitLetters(quotes[i]);
}
function changeQuote() {
  var cw = quoteArray[currentQuote];
  var nw = currentQuote == quotes.length-1 ? quoteArray[0] : quoteArray[currentQuote+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);
  }
  
  currentQuote = (currentQuote == quoteArray.length-1) ? 0 : currentQuote+1;
}
function animateLetterOut(cw, i) {
  setTimeout(function() {
		cw[i].className = 'letter out';
  }, 0);
}
function animateLetterIn(nw, i) {
  setTimeout(function() {
		nw[i].className = 'letter in';
  }, 340+(i*30));
}
function splitLetters(quote) {
  var content = quote.innerHTML;
  console.log(quote.innerHTML);
  quote.innerHTML = '';
  var letters = [];
  for (var i = 0; i < content.length; i++) {
    var letter = document.createElement('span');
    letter.className = 'letter';
    letter.innerHTML = content.charAt(i)==' '?'&nbsp;':content.charAt(i);
    quote.appendChild(letter);
    letters.push(letter);
  }
  
  quoteArray.push(letters);
}
changeQuote();
setInterval(changeQuote, 10000);
body {
  font-weight: 600;
  font-size: 40px;
}
.text {
  position: relative;
}
.quote {
  position: absolute;
  opacity: 0;
}
.letter {
  display: inline-block;
  position: relative;
  float: left;
  -webkit-transform: translateZ(25px);
          transform: translateZ(25px);
  -webkit-transform-origin: 50% 50% 25px;
          transform-origin: 50% 50% 25px;
}
.letter.out {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s 0.7s, opacity 0.7s linear;
}
.letter.behind {
  visibility: hidden;
  opacity: 0;
}
.letter.in {
  visibility: visible;
  opacity: 1;
  transition: opacity 0.7s linear;
}
<body>
<div class="text">
  <p>
    <span class="quote">TEXT ONE(1): For example, if you are designing a brand new website for someone, most times you will have to make sure the prototype looks finished by inserting text or photos or what have you. </span>
    <span class="quote">TEXT TWO(2): The purpose of this is so the person viewing the prototype has a chance to actually feel and understand the idea behind what you have created.</span>
  </p>
  
</div>

【问题讨论】:

    标签: javascript html css effects


    【解决方案1】:

    您的setInterval(changeQuote, 5000) 是您巧妙开发的整理短片效果的来源。最初我开始使用 5000ms 并将其更改为 15000ms 降低到大约 8000~10000ms 似乎使它工作得最好。

    把它改成setInterval(changeQuote, 9000)看看有什么区别。

    但是,考虑到可伸缩性,您需要找到一种方法,以便 setInterval 等到 quoteArray 完成推送字母。

    编辑

    根据 cmets 中的反馈,我确定了以下内容:

    • 在 JavaScript 方面,每个字母都是一个&lt;span&gt;,这意味着每个字母都是一个单独的元素。缺少的是创建一个单词来包裹每个句子。这将确保每个单词都会根据其父容器环绕。

    在 CSS 方面,表示quote 的字母容器需要样式,以便更好地表示其内容。通过添加white-space: nowrap、display: block,我设法为其子项提供了一个容器,该容器将根据屏幕宽度进行调整。

    请参阅下面提供的 sn-p 固定以供参考。

    var quotes = document.getElementsByClassName('quote'),
      quoteArray = [],
      currentQuote = 0;
    
    quotes[currentQuote].style.opacity = 0;
    
    for (var i = 0; i < quotes.length; i++) {
      splitLetters(quotes[i]);
    }
    
    function changeQuote() {
      var cw = quoteArray[currentQuote];
      var nw = currentQuote == quotes.length - 1 ? quoteArray[0] : quoteArray[currentQuote + 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);
      }
    
      currentQuote = (currentQuote == quoteArray.length - 1) ? 0 : currentQuote + 1;
    }
    
    function animateLetterOut(cw, i) {
      setTimeout(function() {
        cw[i].className = 'letter out';
      }, 0);
    }
    
    function animateLetterIn(nw, i) {
      setTimeout(function() {
        nw[i].className = 'letter in';
      }, 340 + (i * 30));
    }
    
    function splitLetters(quote) {
      var content = quote.innerHTML,
      		words = [],
          word = document.createElement('span');
      
      word.className = "word";
      word.innerHTML = "";
      quote.innerHTML = "";
      
      for (var i = 0; i < content.length; i++) {
        var letter = document.createElement('span');
        letter.className = 'letter';
        
        if(content.charAt(i) !== " "){
        	letter.innerHTML = content.charAt(i);
          word.innerHTML = word.innerHTML.concat(letter.innerHTML);
        }
        else {
        	letter.innerHTML = "&nbsp";
          word.innerHTML = word.innerHTML.concat(letter.innerHTML);
          quote.appendChild(word);
          words.push(word);
          word = document.createElement('span');
          word.className = "word";
        }
      }
    
      quoteArray.push(words);
    }
    changeQuote();
    setInterval(changeQuote, 10000);
    body {
      font-weight: 600;
      font-size: 40px;
    }
    
    .text {
      position: relative;
    }
    
    .quote {
      position: absolute;
      display: block;
      opacity: 0;
      white-space: nowrap;
    }
    
    .letter {
      display: inline-block;
      position: relative;
      float: left;
      -webkit-transform: translateZ(25px);
      transform: translateZ(25px);
      -webkit-transform-origin: 50% 50% 25px;
      transform-origin: 50% 50% 25px;
    }
    
    .letter.out {
      visibility: hidden;
      opacity: 0;
      transition: visibility 0s 0.7s, opacity 0.7s linear;
    }
    
    .letter.behind {
      visibility: hidden;
      opacity: 0;
    }
    
    .letter.in {
      visibility: visible;
      opacity: 1;
      transition: opacity 0.7s linear;
    }
    <div class="text">
      <p>
        <span class="quote">TEXT ONE(1): For example, if you are designing a brand new website for someone, most times you will have to make sure the prototype looks finished by inserting text or photos or what have you. </span>
        <span class="quote">TEXT TWO(2): The purpose of this is so the person viewing the prototype has a chance to actually feel and understand the idea behind what you have created.</span>
      </p>
    </div>

    【讨论】:

    • 哦,对不起!我不是在谈论那个。在我的代码中,我实际上就像你说的那样。但是当试图减少我的代码以将其发布到此处时,我将其替换为 5000。无论如何,问题是“如何避免断字?”。如您所见,在某些情况下,单词的某些字母显示在上面的行中,而其余的则显示在下面的行中(单词在两行之间拆分)。这是我要解决的实际问题
    • 哦,我明白了,谢谢!我很困惑,我会调查一下,看看我能为你做些什么:)
    • @Vasilis 我编辑了我的代码,如果您有任何问题,请查看让我知道
    • 谢谢你!很努力!。很明显,如果我尝试逐字呈现文本,那么单词不会中断。在发布问题之前,我还尝试逐字介绍引用 - 就像您的解决方案一样 - 但这不是我想要的结果。这就是为什么我后来尝试按照我在问题中提到的那样逐信。此代码显示引号逐字,而不是逐字母,这就是问题所提到的。
    • @Vasilis 确实!动画本身是我仍在考虑的事情,在午餐和家务之间,所以我会尽力而为今天某个时候:)
    猜你喜欢
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多