自定义事件拖拽组件

<!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>
</head>

<body>
    <div id="div1"></div>
    <div id="div2"></div>
    <div id="div3"></div>
    <div id="div4"></div>
</body>
<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 = ‘toDown‘;
        });

        bindEvent(d3, ‘toMove‘, function(){
            document.title = ‘toMove‘;
        });
        bindEvent(d3, ‘toUp‘, function(){
            document.title = ‘toUp‘;
        });

        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);
                fireEvent(This, "toMove");
            };
            document.onmouseup = function(ev) {
                var ev = ev || window.event;
                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.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 in obj.listeners[events]){
                 obj.listeners[events][i]();
            }
        }
    };

    function extend(child, partent) {
        var child = child || {};
        for(var i in partent) {
            if(typeof partent[i] === "object") {
                child[i] = (partent[i].constructor == Array) ? [] : {};
                extend(child[i], partent[i]);
            }else {
                child[i] = partent[i];
            }
        }
    };

    /*---------------------------
        功能:停止事件冒泡
        ---------------------------*/
    function stopBubble(e) {
        //如果提供了事件对象,则这是一个非IE浏览器
        if ( e && e.stopPropagation )
            //因此它支持W3C的stopPropagation()方法
            e.stopPropagation();
        else
            //否则,我们需要使用IE的方式来取消事件冒泡
            window.event.cancelBubble = true;
    };

    //阻止浏览器的默认行为
    function stopDefault(e) {
        //阻止默认浏览器动作(W3C)
        if ( e && e.preventDefault )
            e.preventDefault();
        //IE中阻止函数器默认动作的方式
        else
            window.event.returnValue = false;
        return false;
    };
</script>
</html>

原文地址:https://www.cnblogs.com/loewe0202/p/8667387.html

时间: 2024-07-30 15:28:07

自定义事件拖拽组件的相关文章

弹窗拖拽组件开发应用

需要注意的问题包括: 1.this的指向到底是指向谁--弄清楚所指的对象 2.深入理解原型的概念及使用: 去改写对象下面公用的方法或者属性 , 让公用的方法或者属性在内存中存在一份 ( 提高性能) 1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <

QT笔记之自定义窗口拖拽移动

1.QT自定义标题栏,拖拽标题栏移动窗口(只能拖拽标题,其他位置无法拖拽) 方法一: 转载:http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html .h文件中 1 //自己重新实现拖动操作 2 protected: 3 4 void mouseMoveEvent ( QMouseEvent * event ); 5 6 void mousePressEvent ( QMouseEvent * event ); 7 8 void mouseRele

iPhone手机解锁效果&amp;&amp;自定义滚动条&amp;&amp;拖拽--Clone&amp;&amp;窗口拖拽(改变大小/最小化/最大化/还原/关闭)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

Android 自定义可拖拽View,界面渲染刷新后不会自动回到起始位置

以自定义ImageView为例: /** * 可拖拽ImageView * Created by admin on 2017/2/21. */ public class FloatingImageView extends ImageView{ public FloatingImageView(Context context) { super(context); } public FloatingImageView(Context context, AttributeSet attrs) { su

H5的拖放事件(拖拽删除)

今天我们来介绍一下h5的拖放事件: 拖放事件 H5的拖放事件提供了多个接口: 1.drag:当元素或者选中的文本被拖动时触发(每几百毫秒触发一次),应用在被拖拽元素上 2.dragend:当拖动操作结束时触发(通过释放鼠标按钮或者点击转义键),应用在被拖拽元素上 3.dragenter:当一个被拖动的元素或者选中的文本进入一个有效的放置目标时触发,应用在目标元素上 4.dragexit:当元素不再是拖动操作的直接选择元素时触发(很少使用) 5.dragleave:当拖动元素或者选中的文本离开有效

vue视频: 自定义指令 &amp;&amp; 拖拽 &amp;&amp; 自定义键盘信息

v-textv-forv-html 指令: 扩展html语法 自定义指令:1. 自定义属性指令: Vue.directive(指令名称,function(参数){ this.el -> 原生DOM元素 // vm.$el }); <div v-red="参数"></div> 指令名称: v-red -> red * 注意: 必须以 v-开头(定义时去掉v-) <!DOCTYPE html> <html lang="en&q

小程序开发如何实现视频或音频自定义可拖拽进度条

程序原生组件的音频播放时并没有进度条的显示,而此次在我们所接的项目中,鉴于原生的视频进度条样式太丑,产品要求做一个可拖拽的进度条满足需求.视频和音频提供的api大致是相似的,可以根据以下代码修改为与音频相关的进度条.wxml的结构如下: <video id="myVideo" src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534

vue2-dragula vue拖拽组件

Drag and drop so simple it hurts Vue wrapper for dragula drag'n drop library, based on vue-dragula by @Astray-git. vue 为 dragula 拖拽 包装 减少 代码,基于   vue-dragula. This library has been refactored, upgraded and extended with powerful new features for use

vue实现拖拽组件

可以拖拽,靠边停靠,效果图如下 代码如下: 注意:代码中使用的图片未上传 DragAndDrop组件: <template> <div class="drag" id="moveDiv" @mousedown="start($event)" @touchstart="start($event)" @mousemove="move($event)" @touchmove="move