js實現拖拽效果

來源:互聯網
上載者:User

js實現拖拽效果

 本文主要是為了讓大家更好的理解js的物件導向,通過實現拖拽效果向大家展示js物件導向,非常不錯,這裡推薦給大家。

 

 

首先來瞭解一下,物件導向練習的基本規則和問題:

先寫出普通的寫法,然後改成物件導向寫法項
普通方法變形

·盡量不要出現函數嵌套函數
·可以有全域變數
·把onload函數中不是賦值的語句放到單獨函數中

改成物件導向

·全域變數就是屬性
·函數就是方法
·onload中建立對象
·改this指標問題

先把拖拽效果的布局完善好:
HTML結構:

<div id="box"></div>

csc樣式:
#box{position: absolute;width: 200px;height: 200px;background: red;}

第一步,首先把面向過程的拖拽回顧一下

 

代碼如下:


window.onload = function (){
// 擷取元素和初始值
var oBox = document.getElementById('box'),
disX = 0, disY = 0;
// 容器滑鼠按下事件
oBox.onmousedown = function (e){
var e = e || window.event;
disX = e.clientX - this.offsetLeft;
disY = e.clientY - this.offsetTop;
document.onmousemove = function (e){
var e = e || window.event;
oBox.style.left = (e.clientX - disX) + 'px';
oBox.style.top = (e.clientY - disY) + 'px';
};
document.onmouseup = function (){
document.onmousemove = null;
document.onmouseup = null;
};
return false;
};
};

 

第二步,把面向過程改寫為物件導向

 

代碼如下:


window.onload = function (){
var drag = new Drag('box');
drag.init();
};
// 建構函式Drag
function Drag(id){
this.obj = document.getElementById(id);
this.disX = 0;
this.disY = 0;
}
Drag.prototype.init = function (){
// this指標
var me = this;
this.obj.onmousedown = function (e){
var e = e || event;
me.mouseDown(e);
// 阻止預設事件
return false;
};
};
Drag.prototype.mouseDown = function (e){
// this指標
var me = this;
this.disX = e.clientX - this.obj.offsetLeft;
this.disY = e.clientY - this.obj.offsetTop;
document.onmousemove = function (e){
var e = e || window.event;
me.mouseMove(e);
};
document.onmouseup = function (){
me.mouseUp();
}
};
Drag.prototype.mouseMove = function (e){
this.obj.style.left = (e.clientX - this.disX) + 'px';
this.obj.style.top = (e.clientY - this.disY) + 'px';
};
Drag.prototype.mouseUp = function (){
document.onmousemove = null;
document.onmouseup = null;
};

 

第三步,看看代碼有哪些不一樣

首頁使用了建構函式來建立一個對象:

 

代碼如下:


// 建構函式Drag
function Drag(id){
this.obj = document.getElementById(id);
this.disX = 0;
this.disY = 0;
}

 

遵守前面的寫好的規則,把全域變數都變成屬性!

然後就是把函數都寫在原型上面:

 

代碼如下:


Drag.prototype.init = function (){
};
Drag.prototype.mouseDown = function (){
};
Drag.prototype.mouseMove = function (){
};
Drag.prototype.mouseUp = function (){
};

 

先來看看init函數:

 

代碼如下:


Drag.prototype.init = function (){
// this指標
var me = this;
this.obj.onmousedown = function (e){
var e = e || event;
me.mouseDown(e);
// 阻止預設事件
return false;
};
};

 

我們使用me變數來儲存了this指標,為了後面的代碼不出現this指向的錯誤

接著是mouseDown函數:

 

代碼如下:


Drag.prototype.mouseDown = function (e){
// this指標
var me = this;
this.disX = e.clientX - this.obj.offsetLeft;
this.disY = e.clientY - this.obj.offsetTop;
document.onmousemove = function (e){
var e = e || window.event;
me.mouseMove(e);
};
document.onmouseup = function (){
me.mouseUp();
}
};

 

改寫成物件導向的時候要注意一下event對象。因為event對象只出現在事件中,所以我們要把event對象儲存變數,然後通過參數傳遞!

後面的mouseMove函數和mouseUp函數就沒什麼要注意的地方了!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.