简易拖拽

心路历程:

老师给我们先写好了一个简易拖拽的封装。一步一步给我们讲解,可谓是幸苦的不行,然而重点不在封装,在细节细节。

代码总览:

      <!doctype html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
        }

        #box2 {
            background: green;
        }

        #box3 {
            background: blue;
        }
    </style>
 </head>
 <body>
    <div id="box1"></div>
    <div id="box2"></div>
    <div id="box3"></div>
    <script src="DragBox.js"></script>
    <script>
        function DragBoxText(boxId) {
            // 继承 DragBox 的属性
            DragBox.call(this, boxId);
        }
        // 继承 DragBox 的方法
        DragBoxText.prototype = new DragBox();
        //  修改了父亲了的方法
        DragBoxText.prototype.move = function(x, y) {
            DragBox.prototype.move.call(this, x, y)
            this.ele.innerHTML = x + ", " + y;
        }
        // 让 box1 具备拖拽的能力
        new DragBoxText("box1");
        new DragBoxText("box2");
        new DragBoxText("box3");
    </script>
 </body>
</html>

简易封装:

function DragBox(boxId) {
    if (boxId == undefined) {
        return;
    }
    // 属性
    this.ele = document.getElementById(boxId);
    var self = this;
    // 因为物体一开始创建就具有拖拽的能力,所以,一开始就进行按下的设置
            this.ele.onmousedown = function(e) {
        self.detaX = e.clientX - self.ele.offsetLeft;
        self.detaY = e.clientY - self.ele.offsetTop;
        // 开始
        self.start();
        // 停止
        document.onmouseup = function() {
            self.stop();
        }
    }
}
方法1: 开始
DragBox.prototype.start = function() {
    var self = this;

    document.onmousemove = function(e) {
        var x = e.clientX - self.detaX;
        var y = e.clientY - self.detaY;

        self.move(x, y)
    }
}
 方法2: 移动
DragBox.prototype.move = function(x, y) {
    var self = this;
    self.ele.style.left = x + "px";
    self.ele.style.top = y + "px";
}
 方法3: 停止
DragBox.prototype.stop = function() {
    document.onmousemove = null;
}
然后引入封装写出要移动的东西。

sript src="DragBox.js"></script> 引入封装

移动的元素自己设定!
引用封包就可以拖拽了。。。
    <div id="box1"></div>
<div id="box2"></div>
<div id="box3"></div>
            new DragBoxText("box1");
    new DragBoxText("box2");
    new DragBoxText("box3");
下来这个就是难点了

在移动的物体上加上坐标,老师一说当场就懵逼了。
老师吧代码敲完是一个懵逼的我上线。
吧细节讲完才稍微领悟那么一丢丢。

function DragBoxText(boxId) {
            // 继承 DragBox 的属性
            DragBox.call(this, boxId);
        }
        // 继承 DragBox 的方法

        DragBoxText.prototype = new DragBox();

DragBox.prototype.move.call(this, x, y)这里可以写成
this.ele.style.left = x + "px";
this.ele.style.top = y + "px";

//  修改了父亲了的方法
        DragBoxText.prototype.move = function(x, y) {
            DragBox.prototype.move.call(this, x, y)
            this.ele.innerHTML = x + ", " + y;
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,368评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • 日本由北海道、本州、九州和四国四个主要岛屿,以及附近若干个大小岛组成,总面积37.8万平方公里,相当于德国或我国云...
    D012希玛太原阅读 414评论 0 1
  • 噩耗是死亡的奴仆,它们像一群厉鬼般将“幸福”和“梦”撕裂,一脚把破碎毫无顾忌的踏为云烟,顺带一口恶臭的黏痰和辱...
    小尹12138阅读 219评论 0 0
  • Snackbar在Android中的使用日益广泛,很大程度上替代了传统的Toast,相比Toast拥有更好的使用体...
    幻海流心阅读 13,843评论 23 71