CSS3部分 - DOM碎片下落效果

雙向 分解 在組合的 效果

<!DOCTYPE html>
<html>
    <head>
            <title>01 PAGE</title>
            <meta name="viewport" content="width=device-width,initial-scale=1">   
            <style type="text/css">
                #main{

                    width: 200px;
                    height: 200px;
                    position:absolute;
                    left: 50%;
                    top:50%;
                    margin: -300px 0 0 -100px;
                    /* background-color: orchid; */
                    /* background-color: white; */
                }

                #main>div{

                    width: 10px;
                    height: 10px;
                    background-color: pink;
                    position:absolute;
                    /* transition: 0.3s ; */
                }
                
            </style> 
    </head>
    <body>
        <div id="main"></div>
        <script type="text/javascript">
            let child;

            for(let i = 0;i<20;i++){
                for(let j = 0;j<20;j++){
                    child = document.createElement("div");
                    child.style.top = (i * 10) + 'px';
                    child.style.left = (j * 10) + 'px';

                    // mubiao 0.1 - 0.3
                    // Math.random() 0 - 1
                    // math,random() * 0.2 0 - 0.2
                    // +0.1 0.1 - 0.3

                    child.style.transition = (Math.random() * 0.2 + 0.1) + 's all ease';
                    // console.log(i,j);
                    main.appendChild(child);
                }
            }

            let index=399;
            let timer;
            // children;
            main.onclick = function(){

                // clearInterval(timer);
                let children = document.querySelectorAll('#main>div');
                if(index==399){
                    clearInterval(timer);
                    timer = setInterval(() => {

                        // if(index == -1){  
                        //     clearInterval(timer);
                        //     // index = 0;
                        //     return;
                        // }
                        
                        children[index].style.top = (children[index].offsetTop + 200) + 'px';
                        index--;
                        if(index == -1){  
                            clearInterval(timer);
                            // index = 0;
                            return;
                        }
                    }, 10);
                    // index = 0;

                }else if(index==-1){
                    clearInterval(timer);
                    // index = 0;
                    timer = setInterval(() => {
                        
                        children[index+1].style.top = (children[index+1].offsetTop - 200) + 'px';
                        index++;
                        if(index == 399){
                            clearInterval(timer);
                            return;
                        }
                    }, 10);
                }

                // let children = document.querySelectorAll('#main>div');
                // // for()
                // // setTimeout(() => {
                // // for(let index=99;index >=0; index--){
                //     // setTimeout(() => {
                //     setInterval(() => {
                        
                //         children[index].style.top = (children[index].offsetTop + 200) + 'px';
                //         index--;
                //     }, 10);
                // // }
                // // }, 300);
            }
        </script>
    </body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章