JavaScript 实现文字动态闪烁切换效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <head>  <style>    :root {        font-s...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<head>  
<style>
    :root {
        font-size: 15px;
    }
    .board {
        background: black;
        color: red;
        display: inline-block;
        padding: 10px;
        border: 2px solid;
        width: 100px;
        text-align: center;
    }
</style>
</head>  
<body>  
<div class="board">
    <span class="text"></span>
</div>
<script>
    let board = document.querySelector('.board');
    let text = board.querySelector('.text');
    let arr = ['第一个', '第二个', '第三个', '第四个', '第五个'];
    let current = 0;
    text.textContent = arr[current];
    boot();
    function boot() {
        startFlash();
    }
    function isVisible() {
        return getComputedStyle(text).opacity != 0;
    }
    function toggleText() {
        if (isVisible()) {
            text.style.opacity = 0;
        } else {
            text.style.opacity = 1;
        }
    }
    function toggleBorder() {
        if (isVisible()) {
            board.style.borderColor = 'transparent';
        } else {
            board.style.borderColor = 'inherit';
        }
    }
    function toggleArr() {
        if (!isVisible()) {
            current++;
            if (current >= arr.length) {
                current = 0;
            }
            text.textContent = arr[current];
        }
    }
    function startFlash() {
        setInterval(function () {
            isVisible();
            toggleBorder();
            toggleArr();
            toggleText();
        }, 500)
    }
</script>
</body>  
</html> 

attachments-2020-06-BhGOJDLu5ef5f5148935b.gif

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
admin
admin

651 篇文章

作家榜 »

  1. admin 651 文章
  2. 粪斗 185 文章
  3. 王凯 92 文章
  4. 廖雪 78 文章
  5. 牟雪峰 12 文章
  6. 李沁雪 9 文章
  7. 全易 2 文章
  8. Stevengring 0 文章