亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 vanilla JS 中的元素中刪除 CSS 動畫

如何從 vanilla JS 中的元素中刪除 CSS 動畫

料青山看我應如是 2021-06-05 16:12:38
我有一個由 nxn 個較小的方形 div 元素組成的方形網格,我想用 CSS 背景顏色動畫按順序照亮這些元素。我有一個函數來為序列生成一個隨機數組。我遇到的問題是,一旦某個方塊被照亮一次,如果它再次出現在陣列中,它就不會第二次照亮。我相信這是因為一旦為元素分配了 CSS 動畫,動畫就無法在該元素上再次觸發,而且我無法找到使其工作的方法。這是我正在學習的響應式 Web 應用程序課程,評估規定我們只能使用 vanilla JS,并且所有元素必須在 JS 中創建并附加到<body>我們的 index.html 中的空白處。根據序列的每個閃光都是通過 setTimeout 函數觸發的,該函數循環遍歷數組中的所有元素,每次循環將其計時器增加 1 秒(動畫長度也是 1 秒)。定義容器和子 div:function createGameContainer(n, width, height) {    var container = document.createElement('div');    //CSS styling    container.style.margin = '50px auto'    container.style.width = width;    container.style.height = height;    container.style.display = 'grid';    // loop generates string to create necessary number of grid columns based on the width of the grid of squares    var columns = '';    for (i = 0; i < n; i++) {        columns += ' calc(' + container.style.width + '/' + n.toString() + ')'    }    container.style.gridTemplateColumns = columns;    container.style.gridRow = 'auto auto';    // gap variable to reduce column and row gap for larger grid sizes    // if n is ever set to less than 2, gap is hardcoded to 20 to avoid taking square root of 0 or a negative value    var gap;    if (n > 1) {        gap = 20/Math.sqrt(n-1);    } else {        gap = 20;    }    container.style.gridColumnGap = gap.toString() + 'px';    container.style.gridRowGap = gap.toString() + 'px';    container.setAttribute('id', 'game-container');    document.body.appendChild(container);}/*function to create individual squares to be appended to parent game container*/function createSquare(id) {    var square = document.createElement('div');    //CSS styling    square.style.backgroundColor = '#333';    //square.style.padding = '20px';    square.style.borderRadius = '5px';    square.style.display = 'flex';    square.style.alignItems = 'center';    //set class and square id    square.setAttribute('class', 'square');    square.setAttribute('id', id);    return square;}
查看完整描述

2 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

試試這個:


function flash(index, delay){

    setTimeout( function() {

        flashingSquare = document.getElementById(index);

        flashingSquare.classList.add('flashing');

        flashingSquare.addEventListener('animationend', function() {

                flashingSquare.classList.remove('flashing');

        }, delay);    

    });

}

不要刪除動畫,刪除 class。


動畫完成后直接刪除類。所以瀏覽器有時間處理所有事情。當您在需要動畫之前直接添加類時,瀏覽器可以觸發所有需要的步驟來執行此操作。


您刪除和添加課程的嘗試很好,但速度很快。我認為瀏覽器和 DOM 優化了你的步驟并且什么都不做。


查看完整回答
反對 回復 2021-06-11
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

經過一番研究,我想出了一個解決方法。我重寫了函數,以便 setTimeout 嵌套在 for 循環中,setTimeout 嵌套在立即調用的函數表達式中(我仍然不完全理解,但是嘿,如果它有效)。新函數如下所示:


/*

function to display game sequence

length can be any integer greater than 1

speed is time between flashes in ms and can presently be set to 1000, 750, 500 and 250.

animation length for each speed is set by a corresponding speed class

in CSS main - .flashing1000 .flashing750 .flashing500 and .flashing250

*/

function displaySequence(length, speed) {

    var sequence = generateSequence(length);

    console.log(sequence);


    for (i = 0; i < sequence.length; i++) {

        console.log(sequence[i]);

        //       immediately invoked function expression

        (function(i) {

            setTimeout( function () {

                var sq = document.getElementById(sequence[i]);

                sq.classList.add('flashing' + speed.toString());

                sq.addEventListener('animationend', function() {

                    sq.classList.remove('flashing' + speed.toString());

                })

            }, (speed * i))

        })(i);

    }

}

每個類的 CSS:


@keyframes flash {

    0% {

        background: #333;

    }


    50% {

        background: orange

    }


    100% {

        background: #333;

    }

}


.flashing1000 {

    animation: flash 975ms;

}


.flashing750 {

    animation: flash 725ms;

}


.flashing500 {

    animation: flash 475ms;

}


.flashing250 {

    animation: flash 225ms;

}

我知道有一些懶惰的解決方法,但效果很好。


查看完整回答
反對 回復 2021-06-11
  • 2 回答
  • 0 關注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號