【animate】animate 動畫 -- 任意個圖形呈圓形散開,動態計算位置

效果 GIF

在這裏插入圖片描述

在線效果

代碼

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>

<style>
    body {
        width: 99%;
        height: 97%;
        background-color: antiquewhite;
    }

    /* btn 父級元素 */
    .box_main {
        text-align: center;
    }

    /* 僞元素輔助對齊 實現垂直居中 */
    .box_main::after {
        content: '';
        display: inline-block;
        vertical-align: middle;
        height: 100%;
    }

    /* 圓形 */
    .radio {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        position: absolute;
        opacity: 0;
        /* (radio.width - box_btn.width) / 2 = (100 - 10) / 2  */
        left: -45;
        top: -45;
    }

    /* 按鈕定位 水平垂直居中 position:relative 屬性 實現其餘標籤的相對定位 */
    .box_btn {
        display: inline-block;
        vertical-align: middle;
        opacity: 1;
        width: 10px;
        height: 10px;
        position: relative;
    }

    .btn {
        position: absolute;
        background-color: brown;
        width: 100%;
        height: 100%;
        border-radius: 50%;
        z-index: 9;
    }
</style>


<body>
    <button onclick="addDom()">點擊此處隨機生成n個圖形</button>
    <span>點擊屏幕中心紅點開始動畫</span>
    <div class="box_main">
        <div class="box_btn">
            <div class="btn">
                <span></span>
            </div>
        </div>
    </div>
</body>

<script>
    $('.btn').click(animate);

    var flag = true;

    function addDom(count) {
        var random = count || Math.floor(Math.random() * 14 + 2);
        var random16 = function () {
            var color = "#";
            for (var i = 0; i < 6; i++) {
                color += (Math.random() * 16 | 0).toString(16);
            }
            return color;
        }
        $('.animate').remove();
        for (let index = 0; index < random; index++) {
            const animate_id = 'animate_' + index;
            const animate_text = 'animate_' + index;
            const randomColor = random16();
            $('.box_btn').append($('<div id=\'' + animate_id + '\' style=\'background-color:' +
                randomColor +
                '\'>' + '' + '</div>').addClass('animate').addClass(
                'radio'));
        }
    }
    $(function () {
        addDom();
    })

    function animate() {
        let _animate = $('.animate');
        let radius = 200;
        $.each(_animate, function (i, ele) {
            let left = Math.cos(2 * Math.PI / _animate.length * i) * radius - ($(this).width() / 2);
            let top = Math.sin(2 * Math.PI / _animate.length * i) * radius - ($(this).height() / 2);
            $(this).animate({
                'left': flag ? left : '-45',
                'top': flag ? top : '-45',
                'opacity': flag ? '1' : '0',
            }, {
                duration: 3000,
                easing: 'swing'
            })
        })
        flag = !flag;
    }
</script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章