簡易拖拽

心路歷程:

老師給我們先寫好了一個(gè)簡易拖拽的封裝。一步一步給我們講解,可謂是幸苦的不行,然而重點(diǎn)不在封裝,在細(xì)節(jié)細(xì)節(jié)。

代碼總覽:

      <!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;
    // 因?yàn)槲矬w一開始創(chuàng)建就具有拖拽的能力,所以,一開始就進(jìn)行按下的設(shè)置
            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: 移動(dòng)
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;
}
然后引入封裝寫出要移動(dòng)的東西。

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

移動(dòng)的元素自己設(shè)定!
引用封包就可以拖拽了。。。
    <div id="box1"></div>
<div id="box2"></div>
<div id="box3"></div>
            new DragBoxText("box1");
    new DragBoxText("box2");
    new DragBoxText("box3");
下來這個(gè)就是難點(diǎn)了

在移動(dòng)的物體上加上坐標(biāo),老師一說當(dāng)場就懵逼了。
老師吧代碼敲完是一個(gè)懵逼的我上線。
吧細(xì)節(jié)講完才稍微領(lǐng)悟那么一丟丟。

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;
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,163評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,422評論 4 61
  • 日本由北海道、本州、九州和四國四個(gè)主要島嶼,以及附近若干個(gè)大小島組成,總面積37.8萬平方公里,相當(dāng)于德國或我國云...
    D012?,斕?/span>閱讀 438評論 0 1
  • 噩耗是死亡的奴仆,它們像一群厲鬼般將“幸福”和“夢”撕裂,一腳把破碎毫無顧忌的踏為云煙,順帶一口惡臭的黏痰和辱...
    小尹12138閱讀 231評論 0 0
  • Snackbar在Android中的使用日益廣泛,很大程度上替代了傳統(tǒng)的Toast,相比Toast擁有更好的使用體...
    幻海流心閱讀 14,131評論 23 71

友情鏈接更多精彩內(nèi)容