【问题标题】:Trouble making an animated HTML title制作动画 HTML 标题时遇到问题
【发布时间】:2021-01-04 18:33:43
【问题描述】:


这是我在这里的第一个问题。我正在努力使我的投资组合更有趣。所以我试着做一个让第一个字母出现在最后的标题。制作标题移动的效果。
从我的角度来看,我在移动方面做得很好。但问题是代码删除了标题中单词之间的空格。
也许我做错了什么,但我不知道是什么。
如果这有帮助,我正在使用谷歌浏览器。
这是我正在使用的代码。

function titleAnim() {
  title = document.title;
  for (let i = 0; i < title.length; i = title.length) {
    let lastChar = title[i];
    let newTitle = title.slice(1);
    newTitle = newTitle + lastChar;
    document.title = newTitle;
  }
};
setInterval(titleAnim, 500);

【问题讨论】:

  • 如果您的循环只执行一次迭代,则不需要循环。
  • 我也看不到任何可以删除空格的逻辑。请创建问题的minimal reproducible example
  • 我已经测试过了。似乎已删除空格。
  • 顺便说一下for循环有错别字,最后应该是i ++,不然不会循环

标签: javascript html animation whitespace title


【解决方案1】:

你不需要那个 for 循环; 这很好

var title = "abc defg" + " "; // add a space at the end so it won't be continous
function titleAnim() {
    let lastChar = title[0];
    let newTitle = title.slice(1);
    title = newTitle = newTitle + lastChar;
    console.log(newTitle);
};
setInterval(titleAnim, 777);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2013-05-02
    • 2021-07-17
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多