js组件化团队开发之自定义事件demo

本demo是div鼠标拖动事件,有点前端经验的都可以看懂

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>无标题文档</title>
    <style>
        #div1 {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
        }

        #div2 {
            width: 100px;
            height: 100px;
            background: yellow;
            position: absolute;
            left: 100px;
        }

        #div3 {
            width: 100px;
            height: 100px;
            background: blue;
            position: absolute;
            left: 200px;
        }

        #div4 {
            width: 100px;
            height: 100px;
            background: green;
            position: absolute;
            left: 300px;
        }
    </style>
    <script>

        //组件开发 : 多组对象,像兄弟之间的关系( 代码复用的一种形式 )

        window.onload = function () {

            var d1 = new Drag();
            d1.init({    //配置参数
                id: 'div1'
            });

            var d2 = new Drag();
            d2.init({   //配置参数
                id: 'div2'
            });

            bindEvent(d2, 'toDown', function () {
                document.title = 'hello';
            });

            bindEvent(d2, 'toDown', function () {
                document.body.style.background = 'black';
            });

            var d3 = new Drag();
            d3.init({    //配置参数
                id: 'div3'
            });

            bindEvent(d3, 'toDown', function () {
                document.title = '妙味';
            });

            bindEvent(d3, 'toUp', function () {
                document.title = '课堂';
            });

            var d4 = new Drag();
            d4.init({    //配置参数
                id: 'div4'
            });

            bindEvent(d4, 'toUp', function () {
                document.title = 'byebye';
            });

        };

        function Drag() {
            this.obj = null;
            this.disX = 0;
            this.disY = 0;

            this.settings = {   //默认参数

            };

        }
        Drag.prototype.init = function (opt) {

            var This = this;

            this.obj = document.getElementById(opt.id);

            extend(this.settings, opt);

            this.obj.onmousedown = function (ev) {
                var ev = ev || window.event;
                This.fnDown(ev);

                fireEvent(This, 'toDown');

                document.onmousemove = function (ev) {
                    var ev = ev || window.event;
                    This.fnMove(ev);
                };
                document.onmouseup = function () {
                    This.fnUp();

                    fireEvent(This, 'toUp');

                };
                return false;
            };

        };

        Drag.prototype.fnDown = function (ev) {
            this.disX = ev.clientX - this.obj.offsetLeft;
            this.disY = ev.clientY - this.obj.offsetTop;
        };
        Drag.prototype.fnMove = function (ev) {
            this.obj.style.left = ev.clientX - this.disX + 'px';
            this.obj.style.top = ev.clientY - this.disY + 'px';
        };
        Drag.prototype.fnUp = function () {
            document.onmousemove = null;
            document.onmouseup = null;
        };


        function bindEvent(obj, events, fn) {
            //obj : 楼层
            //events : 书架
            //fn : 一本书

            obj.listeners = obj.listeners || {};
            obj.listeners[events] = obj.listeners[events] || [];

            obj.listeners[events].push(fn);

            if (obj.nodeType) {
                if (obj.addEventListener) {
                    obj.addEventListener(events, fn, false);
                }
                else {
                    obj.attachEvent('on' + events, fn);
                }
            }
        }

        function fireEvent(obj, events) {   //主动触发自定义事件

            if (obj.listeners && obj.listeners[events]) {
                for (var i = 0; i < obj.listeners[events].length; i++) {
                    obj.listeners[events][i]();
                }
            }

        }


        function extend(obj1, obj2) {
            for (var attr in obj2) {
                obj1[attr] = obj2[attr];
            }
        }


    </script>
</head>

<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,812评论 25 709
  • 独自在外拼搏,除了坚强,别无选择。 几天前,阿林带着哭声给我打了电话,电话里大致就是向我哭诉后悔了,后悔当初不听家...
    墨兮晗阅读 661评论 0 1
  • 第一天的自我工作计划,以下雨天告终,不知是喜是悲。
    黄英涛阅读 260评论 1 1