18 - 五彩小球 - 面向对象


HTML结构

<div id="ball"></div>

Css样式

<style>
        * {
            margin: 0;
            padding: 0;
            border: 0;
        }

        html, body {
            width: 100%;
            height: 100%;
        }

        #ball {
            width: 100%;
            height: 100%;
            background-color: #000;
        }
</style>

Js代码

// Underscore是一个类库,上面有好多方法,网上可以下载到
<script src="js/Underscore-min.js"></script>
// 外链一个面向对象文件
<script src="js/ColorBall.js"></script>
/**
 * 1.构造函数
 * @param option 可选参数
 * @constructor
 */

function ColorBall(option) {
    this._init(option);
}


/**
 * 2.构造函数的原型库
 * @type {{constructor: ColorBall}}  构造器:构造函数
 */

ColorBall.prototype = {

    // 获得当前构造函数
    constructor: ColorBall,

    // 1. 添加属性
    _init: function (option) {
        var option = option || {};
        // 1.1 必要的属性
        this.parentId = option.parentId;
        this.x = option.x || 0;
        this.y = option.y || 0;
        this.r = 60;
        this.bgColor = option.bgColor || 'green';

        // 1.2 变化的属性
        this.dX = _.random(-5, 5);
        this.dY = _.random(-5, 5);
        this.dR = _.random(1, 3);

        // 1.3 把实例化的小球装入数组
        ballArr.push(this);
    },

    // 2. 绘制小球
    render: function () {

        // 2.1 创建节点
        var parentNode = document.getElementById(this.parentId);
        var childNode = document.createElement('div');
        parentNode.appendChild(childNode);

        // 2.2 设置定位
        parentNode.style.position = 'relative';
        childNode.style.position = 'absolute';

        // 2.3 设置属性
        childNode.style.left = this.x + 'px';
        childNode.style.top = this.y + 'px';
        childNode.style.width = this.r + 'px';
        childNode.style.height = this.r + 'px';
        childNode.style.borderRadius = '50%';
        childNode.style.backgroundColor = this.bgColor;
    },

    // 3. 小球开始运动
    update: function () {

        // 3.1 小球位置的变化轨迹
        this.x += this.dX;
        this.y += this.dY;
        this.r -= this.dR;

        // 3.2 判断半径值
        if (this.r < 0) {
            this.r = 0;

            // 如果小球的半径小于0的话 就把小球移除数组
            ballArr = _.without(ballArr, this);
        }
    }
}


<script>
    // 1. 获取标签
    var ball = document.getElementById('ball');

    // 2. 定义小球数组和颜色数组
    var ballArr = [];
    var colorArr = ['pink', 'red', 'purple', 'blue', 'green', 'orange', 'skyblue', 'white'];

    // 3. 创建小球装入数组
    ball.onmousemove = function (event) {
        var event = event || window.event;
        new ColorBall({
            parentId: 'ball',
            x: event.clientX,
            y: event.clientY,
            bgColor: colorArr[_.random(0, colorArr.length - 1)]
        });
    }

    // 4. 开始定时器
    setInterval(function () {
        // 4.1 先把创建的div清屏
        for (var i = 0; i < ball.children.length; i++) {
            ball.removeChild(ball.children[i]);
        }

        // 4.2 绘制小球和小球的动画
        for (var i = 0; i < ballArr.length; i++) {
            ballArr[i].render();
            ballArr[i].update();
        }
    }, 50);
</script>

特效展示

五彩小球
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,693评论 1 92
  • 一:在制作一个Web应用或Web站点的过程中,你是如何考虑他的UI、安全性、高性能、SEO、可维护性以及技术因素的...
    Arno_z阅读 5,036评论 0 1
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,667评论 4 61
  • CSS基础 本文包括CSS基础知识选择器(重要!!!)继承、特殊性、层叠、重要性CSS格式化排版单位和值盒模型浮动...
    廖少少阅读 8,546评论 0 40
  • #morning[玫瑰][玫瑰][玫瑰]# You are remembered for the rules yo...
    帅气的靖王阅读 1,720评论 0 0

友情链接更多精彩内容