【问题标题】:loop through array in javascript for wixsite在wixsite的javascript中循环遍历数组
【发布时间】:2020-07-17 02:16:48
【问题描述】:

我试图获得进入我的 wixsite 的单词“旋转门”的效果,只是被下一个单词替换。

我想这样做的方法是在 javascript 中创建一个包含我想要输入的单词的数组,然后循环遍历该数组,使用我选择的文本动画将每个单词显示在屏幕上。

我有一个需要一个单词的代码,然后将它“打字”到我的 wix 网站上,但是我如何调整它以对许多单词执行相同的操作,一个接一个地退出屏幕(几乎就像单词幻灯片一样输入了吗?)

这是我的代码:

> import wixData from 'wix-data';
> 
> let interval; let timeInterval = 350; let typeStr = "Invest"; let
> typeIdx; let htmlStr = ''; let displayStr;
> 
> $w.onReady(function () {

// Get the original html string, and split it into two pieces.
// In the process, remove the separator !##!
// By saving the original html, we preserve the style of the text to display.
if (htmlStr.length === 0) { // just get the original html text the first time
    htmlStr = $w("#typewriterPage").html;
}
$w("#typewriterPage").html = ''; // don't show the original text
let strPieces = htmlStr.split("!##!");

displayStr = ''; // init string that we will "type" to
typeIdx = 0; // start at beginning of string we want to "type"

// Declare an interval function that will run in the background.
// This function will be triggered by the defined timeInterval.
interval = setInterval(function () {
    // Get next char from string to be typed and concatenate to the display string.
    displayStr = displayStr + typeStr[typeIdx++];
    // Make a sandwich with the display string in between the original html pieces.
    $w("#typewriterPage").html = strPieces[0] + displayStr + strPieces[1];
    // Stop the interval from running when we get to the last character to by "typed".
    if (typeIdx === typeStr.length) clearInterval(interval);
}, timeInterval);
});

【问题讨论】:

    标签: javascript arrays animation text velo


    【解决方案1】:

    const $w = document.querySelector.bind(document);
    
    Object.defineProperty($w('#typewriterPage'), 'html', {
      get: function () { return this.innerHTML },
      set: function (html) { this.innerHTML = html }
    });
    // ignore everything above this comment
    
    const typewriterPage = $w('#typewriterPage');
    
    const carouselWords = ['Invest', 'Finance', 'Borrow', 'Lend'];
    
    typeWords(carouselWords, typewriterPage.html.split('!##!'));
    
    async function typeWords(words, style) {
      while (true) {
        words.unshift(words.pop());
        const [word] = words;
        await typeWord(word, style);
      }
    }
    
    function typeWord(word, style) {
      const chars = [...word];
      let stack = '';
      return new Promise(resolve => {
        const interval = setInterval(() => {
          if (!chars.length) {
            resolve();
            clearInterval(interval);
            return;
          }
          stack += chars.shift();
          typewriterPage.html = style[0] + stack + style[1];
        }, 250);
      });
    }
    <span id='typewriterPage'><u>!##!</u></span>

    【讨论】:

    • 这在预览时适用于我的代码,但是当我发布网站时,只有“!##!”看到了,怎么解决?
    • 在您的控制台中写入$w("#typewriterPage").html,点击Enter,然后分享它所说的内容(不运行动画代码)。
    • 不确定我理解你的意思,我将它粘贴在控制台中的其余代码下。当我按下回车键时它没有给我一个错误。我应该将它粘贴到其他地方吗?
    • 我的目的是找出 idtypewriterPage 的元素的默认 HTML 内容是什么。
    • 我相信是 !##!,这是网站发布时显示的内容,而不是“触发”到单词数组
    猜你喜欢
    • 2022-08-01
    • 2018-12-18
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多