JS面向?qū)ο缶幊虒?shí)現(xiàn)的拖拽功能案例詳解
本文實(shí)例講述了JS面向?qū)ο缶幊虒?shí)現(xiàn)的拖拽功能。分享給大家供大家參考,具體如下:
原始的面向過(guò)程代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
#box {
width: 100px;
height: 100px;
background: blue;
position: absolute;
}
</style>
<title>拖拽</title>
<script>
var oBox=null;
var disX=0;
var disY=0;
window.onload=function(){
oBox=document.getElementById('box');
oBox.onmousedown=fnDown;
};
//鼠標(biāo)按下事件
function fnDown(ev){
var oEvent = ev||event;
disX = oEvent.clientX - oBox.offsetLeft;
disY = oEvent.clientY - oBox.offsetTop;
document.onmousemove = fnMove;
document.onmouseup = fnUp;
}
//鼠標(biāo)移動(dòng)事件
function fnMove(ev){
var oEvent=ev||event;
oBox.style.left = oEvent.clientX - disX + 'px';
oBox.style.top = oEvent.clientY - disY + 'px';
}
//鼠標(biāo)抬起事件
function fnUp(){
document.onmousemove = null;
document.onmouseup = null;
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>
下面是面向?qū)ο蟮拇a
drag.js
/**
* 拖拽
* @param {Object} id div的id
*/
function Drag(id){
this.oBox = document.getElementById(id);
this.disX = 0;
this.disY = 0;
var _this = this;
this.oBox.onmousedown = function(){
_this.fnDown();
}
}
//鼠標(biāo)按下
Drag.prototype.fnDown = function(ev){
var oEvent = ev || event;
this.disX = oEvent.clientX - this.oBox.offsetLeft;
this.disY = oEvent.clientY - this.oBox.offsetTop;
var _this = this;
document.onmousemove = function(){
_this.fnMove();
};
document.onmouseup = function(){
_this.fnUp();
};
}
//鼠標(biāo)移動(dòng)
Drag.prototype.fnMove = function(ev){
var oEvent= ev || event;
this.oBox.style.left = oEvent.clientX - this.disX + 'px';
this.oBox.style.top = oEvent.clientY - this.disY + 'px';
}
//鼠標(biāo)抬起
Drag.prototype.fnUp = function(){
document.onmousemove = null;
document.onmouseup = null;
}
drag.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
div {
position: absolute;
}
</style>
<title>拖拽</title>
<script type="text/javascript" src="../js/drag.js" ></script>
<script>
window.onload = function(){
var drag1 = new Drag("box1");
var drag1 = new Drag("box2");
};
</script>
</head>
<body>
<div id="box1" style="background: red;width: 200px;height: 200px;"></div>
<div id="box2" style="background: blue;width: 100px;height: 100px;"></div>
</body>
</html>


此拖拽有一個(gè)問(wèn)題,就是沒(méi)有控制拖拽出邊界的問(wèn)題。但我們又不想去修改代碼,那我們?cè)趺醋觯繉W(xué)過(guò)java的應(yīng)該都知道可以寫一個(gè)子類來(lái)做一些更加具體的操作,又保留了父類的功能,就是繼承。
html
<script type="text/javascript" src="../js/drag.js" ></script>
<script type="text/javascript" src="../js/dragLimit.js" ></script>
<script>
window.onload = function(){
var drag1 = new Drag("box1");
var drag1 = new DragLimit("box2");//藍(lán)色是不會(huì)超出邊界的
};
</script>
<body>
<div id="box1" style="background: red;width: 200px;height: 200px;"></div>
<div id="box2" style="background: blue;width: 100px;height: 300px;"></div>
</body>
DragLimit.js:DragLimit繼承自Drag,控制了不能出邊界
/**
* 限制邊界的拖拽,繼承自Drag
* @param {Object} id
*/
function DragLimit(id){
Drag.call(this, id);
}
//繼承方法
for(var p in Drag.prototype){
DragLimit.prototype[p] = Drag.prototype[p];
}
/**
* 覆寫父類的鼠標(biāo)移動(dòng)方法,控制不能移出邊界
*/
DragLimit.prototype.fnMove = function(ev){
var oEvent= ev || event;
var left = oEvent.clientX - this.disX;
var top = oEvent.clientY - this.disY;
//控制邊界
if(left < 0){
left = 0;
} else if(left > document.documentElement.clientWidth-this.oBox.offsetWidth){
left = document.documentElement.clientWidth-this.oBox.offsetWidth;
}
if(top <= 0){
top = 0;
} else if(top > document.documentElement.clientHeight-this.oBox.offsetHeight){
top = document.documentElement.clientHeight-this.oBox.offsetHeight;
}
this.oBox.style.left = left + 'px';
this.oBox.style.top = top + 'px';
}
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
JavaScript內(nèi)置對(duì)象math,global功能與用法實(shí)例分析
這篇文章主要介紹了JavaScript內(nèi)置對(duì)象math,global功能與用法,結(jié)合實(shí)例形式分析了javascript中內(nèi)置對(duì)象math與global的基本概念、功能及使用方法,需要的朋友可以參考下2019-06-06
js實(shí)現(xiàn)一個(gè)猜數(shù)字游戲
本文主要介紹了js實(shí)現(xiàn)一個(gè)猜數(shù)字游戲的實(shí)例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03
JavaScript中獲取高度和寬度函數(shù)總結(jié)
這篇文章主要介紹了JavaScript中獲取高度和寬度函數(shù)總結(jié),例如獲取視窗大小、可見(jiàn)區(qū)域?qū)?、可?jiàn)區(qū)域高、獲取元素自身大小等,很方便的一個(gè)總結(jié),需要的朋友可以參考下2014-10-10
three.js中3D視野的縮放實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了three.js中3D視野的縮放實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
JavaScript中發(fā)布/訂閱模式的簡(jiǎn)單實(shí)例
這篇文章主要介紹了JavaScript中發(fā)布/訂閱模式的簡(jiǎn)單實(shí)例,本文給出了一個(gè)簡(jiǎn)單易懂的實(shí)現(xiàn)代碼,比較容易理解,需要的朋友可以參考下2014-11-11
微信小程序bindinput與bindsubmit的區(qū)別實(shí)例分析
這篇文章主要介紹了微信小程序bindinput與bindsubmit的區(qū)別,結(jié)合實(shí)例形式分析了微信小程序bindinput與bindsubmit的具體功能、用法及相關(guān)使用區(qū)別,需要的朋友可以參考下2019-04-04
對(duì)js eval()函數(shù)的一些見(jiàn)解
下面小編就為大家?guī)?lái)一篇對(duì)js eval()函數(shù)的一些見(jiàn)解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
微信小程序使用uni-app開(kāi)發(fā)小程序及部分功能實(shí)現(xiàn)詳解
uni-app是一個(gè)使用Vue.js 開(kāi)發(fā)所有前端應(yīng)用的框架,下面這篇文章主要給大家介紹了關(guān)于微信小程序使用uni-app開(kāi)發(fā)小程序及部分功能實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

