如何用ES6新屬性寫“躁動的小球”

???????提到“躁動的小球”對于前端愛好者來說并不陌生,其書寫的方式有很多種,其核心思想不過為針對瀏覽器的left和top值、定時器控制物體多長時間移動多長距離,怎么移動,多大的物體等等...
???????為了防止低版本瀏覽器版本兼容性差,所以我也準備了兩個版本,一個普通版本,一個改寫ES6寫法,其大致寫法如下:
<h2>一:普通版本</h2>
<b>結構部分</b>
<div id="box"></div>
注:先設置一個包圍小球的盒子,當然不設置可以,那以瀏覽器可視窗口為基準。
<b>樣式部分</b>

*{
            margin:0;
            padding:0;
        }
        #box{
            height:500px;
            width:600px;
            margin:20px auto;
            box-shadow: 0 0 3px #000;
            position: relative;
        }
        .ball{
            position: absolute;
            border-radius:50%;

        }

注:簡單設置一下樣式,盒子設置相對定位,小球相對于其設置絕對定位就可以了,小球不需要設置寬高,用JS控制隨機大小和小球的顏色。下面是JS代碼。

function Ball(){
            //小球的寬高
            this.wh = random(10,50);
            //小球的橫向速度
            this.sx = random(-10,10,0);
            //小球的縱向速度
            this.sy = random(-10,10,0);
            //小球的顏色
            this.bg = 'rgb('+random(0,255)+','+random(0,255)+','+random(0,255)+')';
            //小球的位置 x軸
            this.left = random(10,600-50);
            //小球的位置 y軸
            this.top = random(10,500-50);
            this.box = null;
        }
        //小球出現的位置
        Ball.prototype.create = function(){
            //創(chuàng)建一個小球
            this.box = document.createElement('div');
            //獲取創(chuàng)建的小球
            var box = document.getElementById('box');
            //小球距離和盒子左邊的距離
            this.box.style.left = this.left+'px';
            //小球距離和盒子上邊的距離
            this.box.style.top = this.top+'px';
            //小球的寬度
            this.box.style.width = this.wh+'px';
            //小球的高度
            this.box.style.height = this.wh+'px';
            //小球的背景顏色
            this.box.style.background = this.bg;
            //小球的樣式
            this.box.className = 'ball';
            //把小球添加到div盒子上
            box.appendChild(this.box);
        }

        //小球移動
        Ball.prototype.move = function(){
            //小球撞到x軸左右兩邊設置反向移動,防止溢出
            if(this.left < 0 || this.left > 600 - this.wh){
                this.sx *= -1;
            }
            //小球撞到x軸上下兩邊設置反向移動,防止溢出
            if(this.top < 0 || this.top > 500 -this.wh){
                this.sy *= -1;
            }
            //左右移動
            this.left += this.sx;
            //上下移動
            this.top += this.sy;
            //設置盒子左邊的距離
            this.box.style.left = this.left+'px';
            //設置盒子上邊的距離
            this.box.style.top = this.top+'px';
        }

        // 隨機函數,傳入最大值,最小值和不想出現的值
        function random(min,max,not){
            //設置隨機方法
            var result = Math.ceil(Math.random()*(max-min)+min);
            //跳過不想粗現的值
            if(result == not){
                result++;
            }
            //返回隨機值
            return result;
        }

        //多個球
        var balls = [];
        //這里是設置20個球,想出現幾個寫幾個
        for(var i = 0; i < 20;i++){
            //實例化一個要出現的小球對象
            var ball = new Ball();
            //調創(chuàng)建小球方法
            ball.create();
            balls[i] = ball;
        }
        // console.log(balls);
        //設置定時器,這里給了10毫秒
        var timer = setInterval(function(){
            for(var j = 0; j < balls.length; j++){
                balls[j].move();
            }
        },10)

注:在設置多個小球的時候不要給太多,否則很可能會卡的,當然你覺得你GPU足夠強大可以試試,當然你如果非要給很多小球的話,四五千上萬個話,建議用canvas做吧。強大的canvas哈哈。話入正題!上面時普通寫法,基本沒什么難度,寫幾個方法,控制邊緣,調用方法就可以了。下面是改寫的ES6新屬性寫法。
<h2>二:ES6版本</h2>
<h5>樣式和結構不變,使用上面同一個</h5>

class Ball{
            constructor(){
                //球的寬高
                this.wh = random(10,50);
                //球的橫向速度
                this.sx = random(-10,10,0);
                //球的縱向速度
                this.sy = random(-10,10,0);
                //球的顏色
                this.bg = `rgb(${random(0,255)},${random(0,255)},${random(0,255)})`;
                //球的位置 x軸
                this.left = random(10,600-50);
                //球的位置 y軸
                this.top = random(10,500-50);
                this.box = null;
                }
        

            create(){
                this.box = document.createElement('div');
                let box = document.querySelector('#box');
                this.box.style.cssText = `
                left:${this.left}px;
                top:${this.top}px;
                width:${this.wh}px;
                height:${this.wh}px;
                background:${this.bg};`;

                this.box.className = 'ball';
                box.appendChild(this.box);
            }

        //小球移動
        move(){
            if(this.left < 0 || this.left > 600 - this.wh){
                this.sx *= -1;
            }
            if(this.top < 0 || this.top > 500 -this.wh){
                this.sy *= -1;
            }
            this.left += this.sx;
            this.top += this.sy;
            this.box.style.left = this.left+'px';
            this.box.style.top = this.top+'px';
        }
    }
        // 隨機函數,傳入最大值,最小值和不想出現的值
        function random(min,max,not){
            let result = Math.ceil(Math.random()*(max-min)+min);
            if(result == not){
                result++;
            }
            return result;
        }

        //多個球
        let balls = [];
        for(let i = 0; i < 10;i++){
            let ball = new Ball();
            ball.create();
            balls[i] = ball;
        }
        // console.log(balls);

        const timer = setInterval(function(){
            for(let j = 0; j < balls.length; j++){
                balls[j].move();
            }
        },10)

附:注釋和上面普通寫法一樣,這里不多寫了,喜歡的朋友留個贊吧,感謝支持!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 函數參數的默認值 基本用法 在ES6之前,不能直接為函數的參數指定默認值,只能采用變通的方法。 上面代碼檢查函數l...
    呼呼哥閱讀 3,455評論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現,斷路器,智...
    卡卡羅2017閱讀 134,886評論 18 139
  • 一、ES6簡介 ? 歷時將近6年的時間來制定的新 ECMAScript 標準 ECMAScript 6(亦稱 ...
    一歲一枯榮_閱讀 6,113評論 8 25
  • 發(fā)現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,206評論 4 61
  • 當未來還未來 你永遠不知道,期待未來的喜悅和困守于現實的痛苦哪個會先到來!所以當未來還沒來,當現實還無法逃開,那就...
    santanolife閱讀 336評論 0 1