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

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

JS動畫碾壓

JS動畫碾壓

動漫人物 2023-07-20 10:43:33
我正在制作一個應用程序,其中復選框控制覆蓋是否出現在屏幕上。為了使出現和消失平滑,我添加了動畫。#overlay{    position:absolute;    height: 100vh;    width: 100vw;    background-color: white;    display:none;    animation-name: none;    animation-play-state: paused;    animation-fill-mode: forwards;    animation-duration: 1s;    opacity: 0;}@keyframes appear {    0%{opacity: 0}    100%{opacity: 1}}@keyframes disappear {    0%{opacity: 1}    100%{opacity: 0}}那么這里我用js來調用這些動畫。function handleChange(checkbox) {    const animation = document.getElementById("overlay");    if(checkbox.checked === true){        if (window.innerWidth < 846) {            animation.style.display="block";            animation.style.animationName="appear";            animation.style.animationPlayState = "running";            document.getElementById('overlay2').style.display="none";            disableScroll();        }        return false;    }else{        animation.style.animationName="disappear";        animation.style.animationPlayState="running";        animation.addEventListener('animationend', () => {            animation.style.display="none";            document.getElementById("overlay2").style.display="block";        })        enableScroll();        return false;    }}首次選中和取消選中時,動畫效果完美。但是,如果我重復此操作并第二次選中該框,它就會被壓碎。也許有一點幫助?謝謝你!
查看完整描述

1 回答

?
DIEA

TA貢獻1820條經驗 獲得超2個贊

因為您每次都添加了事件偵聽器。


解決方案:


function handleChange(checkbox) {

    const animation = document.getElementById("overlay");

    if(checkbox.checked === true){

        if (window.innerWidth < 846) {

            animation.style.display="block";

            animation.style.animationName="appear";

            animation.style.animationPlayState = "running";

            document.getElementById('overlay2').style.display="none";

            disableScroll();

        }

        return false;

    } else {

        animation.style.animationName="disappear";

        animation.style.animationPlayState="running";

        const singleEvent = () => {

            animation.style.display="none";

            document.getElementById("overlay2").style.display="block";

            animation.removeEventListener('animationend', singleEvent)

        };

        animation.addEventListener('animationend', singleEvent);

        enableScroll();

        return false;

    }

}


查看完整回答
反對 回復 2023-07-20
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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