模拟视频小弹幕。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>弹幕</title>
</head>
<style>
    /* //css */
    * {
        box-sizing: border-box;
        outline: none;
    }

    p {
        margin: .5em;
        word-break: break-all;
    }

    .container {
        position: relative;
        width: 700px;
        height: 500px;
        margin: auto;
        padding-right: 200px;
    }

    .content {
        width: 100%;
        height: 100%;
        border: 1px solid #ccc;
    }

    .content-opt {
        position: absolute;
        top: 0;
        right: 0;
        width: 200px;
        height: 100%;
    }

    .content-text {
        height: calc(100% - 30px);
        margin-bottom: 30px;
        border: 1px solid #ccc;
        overflow: auto;
    }

    .content-input {
        position: absolute;
        bottom: 0;
        width: 100%;
        height: 30px;
        border: 1px solid #ccc;
    }

    .content-input input {
        width: 70%;
        padding: 2px;
        border-radius: 5px;
    }

    .content-input button {
        padding: 3px 10px;
        border: none;
        border-radius: 5px;
        background: rgb(90, 154, 214);
    }
</style>
<body>
<div class="container">
    <div id="content" class="content"></div>
    <div class="content-opt">
        <div id="content-text" class="content-text"></div>
        <div class="content-input">
            <input id="text" type="text">
            <button id="send">发送</button>
        </div>
    </div>
</div>

</body>
<script>
    //js
    (function () {
        class Barrage {
            constructor(id) {
                this.domList = [];
                this.dom = document.querySelector('#' + id);
                if (this.dom.style.position == '' || this.dom.style.position == 'static') {
                    this.dom.style.position = 'relative';
                }
                this.dom.style.overflow = 'hidden';
                let rect = this.dom.getBoundingClientRect();
                this.domWidth = rect.right - rect.left;
                this.domHeight = rect.bottom - rect.top;
            }

            shoot(text) {
                let div = document.createElement('div');
                div.style.position = 'absolute';
                div.style.left = this.domWidth + 'px';
                div.style.top = (this.domHeight - 20) * +Math.random().toFixed(2) + 'px';
                div.style.whiteSpace = 'nowrap';
                div.style.color = '#' + Math.floor(Math.random() * 0xffffff).toString(16);
                div.innerText = text;
                this.dom.appendChild(div);

                let roll = (timer) => {
                    let now = +new Date();
                    roll.last = roll.last || now;
                    roll.timer = roll.timer || timer;
                    let left = div.offsetLeft;
                    let rect = div.getBoundingClientRect();
                    if (left < (rect.left - rect.right)) {
                        this.dom.removeChild(div);
                    } else {
                        if (now - roll.last >= roll.timer) {
                            roll.last = now;
                            left -= 3;
                            div.style.left = left + 'px';
                        }
                        requestAnimationFrame(roll);
                    }
                }
                roll(50 * +Math.random().toFixed(2));
            }

        }

        let barage = new Barrage('content');

        function appendList(text) {
            let p = document.createElement('p');
            p.innerText = text;
            document.querySelector('#content-text').appendChild(p);
        }

        document.querySelector('#send').onclick = () => {
            let text = document.querySelector('#text').value;
            barage.shoot(text);

            appendList(text);
        };

        const textList = ['弹幕', '666', '233333333', 'javascript', 'html', 'css', '前端框架', 'Vue', 'React', 'Angular',
            '测试弹幕效果'
        ];
        textList.forEach((s) => {
            barage.shoot(s);
            appendList(s);
        })
    })()
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值