JS コードを使用して弾幕エフェクトを作成する

php中世界最好的语言
リリース: 2018-03-13 16:37:33
オリジナル
3451 人が閲覧しました

今回はJSコードを使って弾幕エフェクトを作成する方法を紹介します。JSコードを使用して弾幕エフェクトを作成する際の注意点を以下に示します。

実装原理

1. 表示弾幕要素の位置属性を相対に設定します
2. 弾幕要素を動的に作成し、位置属性を絶対に設定し、左は表示幅をランダムに設定します
3.弾幕要素
4. 弾幕要素の移動速度をランダムに生成し、左の値を変更します

ランダムな色

最初の実装では color = '#' + Math.floor(Math.random() * 0xffffff).toString(16) としました。 ;

2 番目の実装では color = '#' + Math.floor(Math.random() * 16777215).toString(16);

3 番目の実装では r = Math.floor(Math.random()*256) ); g = Math.floor(Math.random()*256);let b = Math.floor(Math.random()*256); let color = `rgb(${r},${g}, ${b}) `;

ランダムレート

50 * +Math.random().toFixed(2)
ログイン後にコピー

code

//html

<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>
ログイン後にコピー

//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);
}
ログイン後にコピー

//js

(function () {
    class Barrage {
        constructor(id) {
            this.domList = [];
            this.dom = document.querySelector(&#39;#&#39; + id);            if (this.dom.style.position == &#39;&#39; || this.dom.style.position == &#39;static&#39;) {
                this.dom.style.position = &#39;relative&#39;;
            }
            this.dom.style.overflow = &#39;hidden&#39;;            let rect = this.dom.getBoundingClientRect();
            this.domWidth = rect.right - rect.left;
            this.domHeight = rect.bottom - rect.top;
        }
        shoot(text) {            let div = document.createElement(&#39;div&#39;);
            div.style.position = &#39;absolute&#39;;
            div.style.left = this.domWidth + &#39;px&#39;;
            div.style.top = (this.domHeight - 20) * +Math.random().toFixed(2) + &#39;px&#39;;
            div.style.whiteSpace = &#39;nowrap&#39;;
            div.style.color = &#39;#&#39; + 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 + &#39;px&#39;;
                    }
                    requestAnimationFrame(roll);
                }
            }
            roll(50 * +Math.random().toFixed(2));
        }
    }    let barage = new Barrage(&#39;content&#39;);    function appendList(text) {        let p = document.createElement(&#39;p&#39;);
        p.innerText = text;
        document.querySelector(&#39;#content-text&#39;).appendChild(p);
    }
    document.querySelector(&#39;#send&#39;).onclick = () => {        let text = document.querySelector(&#39;#text&#39;).value;
        barage.shoot(text);
        appendList(text);
    };
    const textList = [&#39;弹幕&#39;, &#39;666&#39;, &#39;233333333&#39;, &#39;javascript&#39;, &#39;html&#39;, &#39;css&#39;, &#39;前端框架&#39;, &#39;Vue&#39;, &#39;React&#39;, &#39;Angular&#39;,        &#39;测试弹幕效果&#39;
    ];
    textList.forEach((s) => {
        barage.shoot(s);
        appendList(s);
    })
})()
ログイン後にコピー

事例を読んだ後、あなたはメソッドをマスターしたと思いますこの記事では、よりエキサイティングな内容に注目してください。その他の関連記事は php 中国語 Web サイトにあります。

推奨読書:

H5 キャンバスを使用して弾幕エフェクトを作成する

H5 キャンバスを使用してホラー アニメーションを作成する

以上がJS コードを使用して弾幕エフェクトを作成するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!