一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

js 模擬氣泡屏保效果代碼

核心代碼:
復(fù)制代碼 代碼如下:
var T$ = function(id) { return document.getElementById(id); }
var $extend = function(des, src) { for (var p in src) { des[p] = src[p]} return des; }
var Bubble = function() {
    // 小球隨機(jī)樣式
    var clss = ['ball_one', 'ball_two',  'ball_three', 'ball_four', 'ball_five', 'ball_six'];
    var Ball = function(radius, clsname) {
        var ball = document.createElement('div');
        ball.className = clsname;
        with(ball.style) {
            width = height = (radius || 10) + 'px';  position = 'absolute';
        }
        return ball;
    };
    // 屏保主類
    var Screen = function(cid, config) {
        var self = this;
        if (!(self instanceof Screen)) {
            return new Screen(cid, config);
        }
        self.container = T$(cid);
        if (!self.container) return;
        config = $extend(Screen.Config, config || {});
        // 配置屬性
        self.ballsnum = config.ballsnum;
        self.diameter = 55;
        self.radius = self.diameter / 2;
        self.bounce = config.bounce;
        self.spring = config.spring;
        self.gravity = config.gravity;
        self.balls = [];
        self.timer = null;
        // 上下左右邊界
        self.T_bound = 0;
        self.B_bound = self.container.clientHeight;
        self.L_bound = 0;
        self.R_bound = self.container.clientWidth;
    };
    // 靜態(tài)屬性
    Screen.Config = {
        ballsnum: 5,   // 小球數(shù)目
        spring: 0.8,   // 彈力加速度
        bounce: -0.95, // 反彈
        gravity: 0.1   // 重力
    };
    Screen.prototype = {
        initialize: function() {
            var self = this;
            // 生成小球
            self.createBalls();
            // 偵聽碰撞
            self.timer = setInterval(function() {
                self.hitTest();
            }, 32);
        },
        createBalls: function() {
            var self = this, num = self.ballsnum, i = 0;
            var frag = document.createDocumentFragment();
            for (; i < num; i++) {
                var ball = new Ball(self.diameter, clss[Math.floor(Math.random() * (clss.length - 1))]);
                ball.radius = self.radius;
                ball.diameter = self.diameter;
                ball.style.left = (Math.random() * self.B_bound) + 'px';
                ball.style.top = (Math.random() * self.R_bound) + 'px';
                ball.vx = Math.random() * 6 - 3;
                ball.vy = Math.random() * 6 - 3;
                frag.appendChild(ball);
                self.balls[i] = ball;
            }
            self.container.appendChild(frag);
        },
        // 碰撞檢測
        hitTest: function() {
            var self = this, num = self.ballsnum, balls = self.balls;
            for (var i = 0; i < num - 1; i++) {
                var ball0 = balls[i];
                ball0.x = ball0.offsetLeft + ball0.radius;
                ball0.y = ball0.offsetTop + ball0.radius;
                for (var j = i + 1; j < num; j++) {
                    var ball1 = balls[j];
                    ball1.x = ball1.offsetLeft + ball1.radius;
                    ball1.y = ball1.offsetTop + ball1.radius;
                    var dx = ball1.x - ball0.x;
                    var dy = ball1.y - ball0.y;
                    var dist = Math.sqrt(dx * dx + dy * dy);
                    var misDist = ball0.radius + ball1.radius;
                    if (dist < misDist) {
                        var angle = Math.atan2(dy, dx);
                        var tx = ball0.x + Math.cos(angle) * misDist;
                        var ty = ball0.y + Math.sin(angle) * misDist;
                        var ax = (tx - ball1.x) * self.spring;
                        var ay = (ty - ball1.y) * self.spring;
                        ball0.vx -= ax;
                        ball0.vy -= ay;
                        ball1.vx += ax;
                        ball1.vy += ay;
                    }
                }
            }
            for (var i = 0; i < num; i++) {
                self.move(balls[i]);
            }
        },
        // 氣泡運(yùn)動
        move: function(ball) {
            var self = this;
            ball.vy += self.gravity;
            ball.style.left = (ball.offsetLeft + ball.vx) + 'px';
            ball.style.top = (ball.offsetTop + ball.vy) + 'px';
            // 邊界檢測
            var T = self.T_bound, B = self.B_bound, L = self.L_bound, R = self.R_bound, BC = self.bounce;
            if (ball.offsetLeft + ball.diameter > R) {
                ball.style.left = R - ball.diameter + 'px';
                ball.vx *= BC;
            } else if (ball.offsetLeft < L) {
                ball.style.left = L + 'px';
                ball.vx *= BC;
            }
            if (ball.offsetTop + ball.diameter > B) {
                ball.style.top = B - ball.diameter + 'px';
                ball.vy *= BC;
            } else if (ball.offsetTop < T) {
                ball.style.top = T + 'px';
                ball.vy *= BC;
            }
        }
    };
    return { Screen: Screen }
}();
window.onload = function() {
    var sc = null;
    T$('start').onclick = function() {
        document.getElementById('inner').innerHTML = '';
        sc = Bubble.Screen('inner', { ballsnum: 5, spring: 0.8, bounce: -0.95, gravity: 0.1});
        sc.initialize();
    };
    T$('stop').onclick = function() { clearInterval(sc.timer); }
    var bound = false
    T$('change').onclick = function() {
        if (!bound) {    
            T$('screen').style.backgroundImage = 'url("http://demo.jb51.NET/js/bubbling/o_bg1.jpg")';
            bound = true;
        } else {
            T$('screen').style.backgroundImage = 'url("http://demo.jb51.NET/js/bubbling/o_bg2.jpg")';
            bound = false;
        }
    }
}

【說明】
程序效率出現(xiàn)了很大瓶頸。需要做的優(yōu)化還有很多。有時(shí)間繼續(xù)完善。
另:感謝羅浮宮群友逍遙君武和豪情對圖片的支持。
【源碼下載】
http://www.jb51.NET/jiaoben/28295.html

JavaScript技術(shù)js 模擬氣泡屏保效果代碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产女乱淫真高清免费视频 | 国产精品二区三区 | 91网址在线播放 | 很黄很暴力深夜爽爽无遮挡 | 91麻豆影视 | 狠狠色噜噜狠狠狠狠97不卡 | 亚洲免费视频一区二区三区 | 国产亚洲精品俞拍视频 | 亚洲免费视频网站 | 日本亚洲精品 | 亚洲美女aⅴ久久久91 | 国产视频自拍一区 | 激情小说激情视频激情图片 | 欧美又黄又嫩大片a级 | a黄毛片| 午夜a毛片 | 免费国产成人手机在线观看 | 久久免费区一区二区三波多野 | 久久综合一区二区三区 | 亚洲成人在线免费观看 | 青青国产成人久久91 | 五月天网站最新永久网址 | 高清国产美女一级a毛片录 高清国产美女一级毛片 | 狂野欧美性猛交xxxx免费按摩 | 高清一区高清二区视频 | 久久精品国产99久久 | 美女胸又www又黄的网站视频 | 国语自产拍在线观看任你躁 | 亚洲小说图片视频 | 亚洲国产欧美精品 | 国产高清视频免费最新在线 | 精品国产夜色在线 | 91热精品 | 免费在线看黄 | 久久入| 91年精品国产福利线观看久久 | 日本www色视频成人免费免费 | 在线成人小视频 | 9久久免费国产精品特黄 | 天天色天天操综合网 | 免费大黄网站在线观看 |