当用户在输入字段中键入搜索值时,代码将判断该值是否出现在任何列表项中.如果是并且该列表项先前被隐藏,则匹配的列表项闪烁绿色并再次出现;如果不是,则匹配的列表项闪烁红色并消失.

然而,出于某种原因,当它们再次出现时应该闪烁一次绿色的匹配物品似乎闪烁了多次(2次或3次).

您可以在此处进行测试: 只需输入123,然后删除3和2

//jshint esnext:true

document.querySelector('input#search_input').addEventListener('input', search_value);
const logTextarea = document.querySelector('#log');

const list_elements = document.querySelectorAll('ul#list_of_stuff li');

list_elements.forEach(li => {
  li.addEventListener('transitionend', transition_ended);
});

function search_value() {
    const searchString = document.querySelector('input#search_input').value.toLowerCase();
    const list_elements = document.querySelectorAll('ul#list_of_stuff li');
    list_elements.forEach(list_element => {
        const list_element_value = list_element.textContent.toLowerCase();

        if (list_element_value.includes(searchString)) {
            if (list_element.className == 'hidden') {
                list_element.className = 'flash_green';
            }
        } else {
            if (list_element.className != 'hidden') {
                list_element.className = 'flash_red';
            }
        }
    });
}

function transition_ended(event) {
    let element = event.target;

    logTextarea.value += `Transition ended for ${element.tagName}, at this point className is ${element.className}\n`;

    if (element.className == 'flash_red') {
        element.className = 'hidden';
    }

    if (element.className == 'flash_green' || element.className == 'flash_red') {
        console.log('Deleting ', element.className);
        logTextarea.value += `Deleting ${element.className}\n`;
        element.className = '';
    }
}
#list_of_stuff li.flash_red {
    animation: flash_red 0.5s;
    animation-iteration-count: 3;
    transition: opacity 0.5s;
    opacity: 0;
}

@keyframes flash_red {
    0% {
        background-color: red;
    }
    50% {
        background-color: transparent;
    }
    100% {
        background-color: red;
    }
}

#list_of_stuff li.flash_green {
    animation: flash_green 0.5s;
    animation-iteration-count: 3;
    opacity: 100;
}

@keyframes flash_green {
    0% {
        background-color: green;
    }
    50% {
        background-color: transparent;
    }
    100% {
        background-color: green;
    }
}

.hidden {
  display: none !important;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Input text and list in HTML</title>
  </head>
  <body>
    <input type="text" id="search_input">
    <ul id="list_of_stuff">
      <li>1</li>
      <li>12</li>
      <li>123</li>
      <li>1234</li>
    </ul>
    
        <textarea id="log" style="min-width: 613px; min-height: 500px;"></textarea>
  </body>
</html>

同样在这里:https://jsbin.com/yonejoqihu/

这就像,当发生flashgreen时,因为类仍然存在,所以没有触发转换结束.不知道为什么?

推荐答案

您的动画(比方说绿色)执行以下操作:

  1. 在开始时将背景设置为绿色
  2. 中途将背景设置为透明
  3. 完成后将背景设置为绿色

所以在一个周期里,有一个绿色的" blink ",然后是白色的,然后又是绿色的.当你重复这三次时,它会变得透明两次,因为在一个循环的结束和下一个循环的开始之间,背景不会改变.

animation-iteration-count设置为1而不是3,您将获得一个周期.

Javascript相关问答推荐

根据总价格对航班优惠数组进行排序并检索前五个结果- Angular HTTP请求

在贝塞尔曲线的直线上找不到交叉点:(使用@Pomax的bezier.js)

当在select中 Select 选项时,我必须禁用不匹配的 Select

Spring boot JSON解析错误:意外字符错误

Angular 17—每当一个布尔变量变为真时触发循环轮询,只要它保持为真

如何使用JavaScript将文本插入空div

阿波罗返回的数据错误,但在网络判断器中是正确的

简单的PayPal按钮集成导致404错误

如何在ASP.NET JavaScript中使用Google Charts API仅对绘制为负方向的条形图移动堆叠条形图标签位置

使用Java脚本导入gltf场景并创建边界框

如何使用JavaScript拆分带空格的单词

如何在ASP.NET项目中使用Google Chart API JavaScript将二次轴线值格式化为百分比

为什么我的自定义元素没有被垃圾回收?

用于部分字符串的JavaScript数组搜索

无法设置RazorPay订阅API项目价格

如何根据输入数量正确显示alert ?

如何在Java脚本中并行运行for或任意循环的每次迭代

固定动态、self 调整的优先级队列

我如何才能获得价值观察家&对象&S的价值?

如何从Reaction-Redux中来自API调用的数据中筛选值