JavaScript使用面向?qū)ο髮?shí)現(xiàn)的拖拽功能詳解
本文實(shí)例講述了JavaScript使用面向?qū)ο髮?shí)現(xiàn)的拖拽功能。分享給大家供大家參考,具體如下:
面向?qū)ο笥袀€(gè)前提:
- 前提:所有東西都必須包含在onload里
- 改寫(xiě):不能有函數(shù)嵌套,可以有全局變量
- 過(guò)程,如下
- onload改成構(gòu)造函數(shù),
- 全局變量改成屬性(通過(guò)this)
- 函數(shù)改寫(xiě)成方法
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>面向?qū)ο蟮睦^承-1</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
</style>
<script>
window.onload = function() {
var oDiv = document.getElementById('div1');
oDiv.onmousedown = function(ev) {
var ev = ev || event;
var disX = ev.clientX - this.offsetLeft;
var disY = ev.clientY - this.offsetTop;
document.onmousemove = function(ev) {
var ev = ev || event;
oDiv.style.left = ev.clientX - disX + 'px';
oDiv.style.top = ev.clientY - disY + 'px';
}
document.onmouseup = function() {
document.onmousemove = document.onmouseup = null;
}
}
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
把局部變量改成全局變量
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>面向?qū)ο蟮睦^承-2</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
</style>
<script>
var oDiv=null;
var disX=0;
var disY=0;
window.onload = function() {
oDiv = document.getElementById('div1');
oDiv.onmousedown = fnDown;
}
function fnMove(ev) {
var ev = ev || event;
oDiv.style.left = ev.clientX - disX + 'px';
oDiv.style.top = ev.clientY - disY + 'px';
}
function fnUp() {
document.onmousemove = document.onmouseup = null;
}
function fnDown(ev) {
var ev = ev || event;
disX = ev.clientX - this.offsetLeft;
disY = ev.clientY - this.offsetTop;
document.onmousemove = fnMove;
document.onmouseup =fnUp;
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
引用塊內(nèi)容
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>面向?qū)ο蟮睦^承-2</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
#div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;}
</style>
<script>
window.onload=function(){
new Drag('div1');
new Drag('div2');
}
function Drag(id) {
var _this=this;
this.disX=0;
this.disY=0;
this.oDiv = document.getElementById(id);
this.oDiv.onmousedown = function(){
_this.fnDown()
};
}
Drag.prototype.fnDown=function (ev) {
var ev = ev || event;
var _this=this;
this.disX = ev.clientX - this.oDiv.offsetLeft;
this.disY = ev.clientY - this.oDiv.offsetTop;
document.onmousemove = function(){
_this.fnMove();
};
document.onmouseup =function(){
_this.fnUp();
};
}
Drag.prototype.fnMove=function(ev) {
var ev = ev || event;
this.oDiv.style.left = ev.clientX - this.disX + 'px';
this.oDiv.style.top = ev.clientY - this.disY + 'px';
}
Drag.prototype.fnUp=function () {
document.onmousemove = null;
document.onmouseup = null
}
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>面向?qū)ο蟮睦^承-2</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
#div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;}
</style>
<script>
window.onload=function(){
new Drag('div1');
new Drag('div2');
}
function Drag(id) {
var _this=this;
this.disX=0;
this.disY=0;
this.oDiv = document.getElementById(id);
this.oDiv.onmousedown = function(){
_this.fnDown()
};
}
Drag.prototype.fnDown=function (ev) {
var ev = ev || event;
var _this=this;
this.disX = ev.clientX - this.oDiv.offsetLeft;
this.disY = ev.clientY - this.oDiv.offsetTop;
document.onmousemove = function(){
_this.fnMove();
};
document.onmouseup =function(){
_this.fnUp();
};
}
Drag.prototype.fnMove=function(ev) {
var ev = ev || event;
this.oDiv.style.left = ev.clientX - this.disX + 'px';
this.oDiv.style.top = ev.clientY - this.disY + 'px';
}
Drag.prototype.fnUp=function () {
document.onmousemove = null;
document.onmouseup = null
}
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試一下運(yùn)行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript頁(yè)面元素操作技巧總結(jié)》、《JavaScript操作DOM技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JS基于面向?qū)ο髮?shí)現(xiàn)的拖拽功能示例
- JS基于面向?qū)ο髮?shí)現(xiàn)的拖拽庫(kù)實(shí)例
- jquery方法+js一般方法+js面向?qū)ο蠓椒▽?shí)現(xiàn)拖拽效果
- Sortable.js拖拽排序使用方法解析
- js實(shí)現(xiàn)拖拽效果
- javascript實(shí)現(xiàn)移動(dòng)端上的觸屏拖拽功能
- 使用js實(shí)現(xiàn)的簡(jiǎn)單拖拽效果
- js完美的div拖拽實(shí)例代碼
- javascript支持firefox,ie7頁(yè)面布局拖拽效果代碼
- JS面向?qū)ο缶幊虒?shí)現(xiàn)的拖拽功能案例詳解
相關(guān)文章
關(guān)于動(dòng)態(tài)執(zhí)行代碼(js的Eval)實(shí)例詳解
下面小編就為大家?guī)?lái)一篇關(guān)于動(dòng)態(tài)執(zhí)行代碼(js的Eval)實(shí)例詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
微信小程序tabBar組件切換與下拉刷新實(shí)現(xiàn)詳解
tabBar相對(duì)而言用的還是比較多的,但是用起來(lái)并沒(méi)有難,下面這篇文章主要給大家介紹了關(guān)于微信小程序全局配置之tabBar的相關(guān)資料,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
深入理解js A*尋路算法原理與具體實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了js A*尋路算法原理與具體實(shí)現(xiàn)過(guò)程,結(jié)合實(shí)例形式詳細(xì)分析了A*尋路算法的具體概念、原理、實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
JS獲取指定時(shí)間的時(shí)間戳的方法匯總(最新整理收藏版)
在JavaScript中,可以使用Date.parse()或new Date()來(lái)獲取指定時(shí)間的時(shí)間戳,本文給大家分享JS獲取指定時(shí)間的時(shí)間戳的方法,感興趣的朋友一起看看吧2024-01-01
詳解wepy開(kāi)發(fā)小程序踩過(guò)的坑(小結(jié))
這篇文章主要介紹了詳解wepy開(kāi)發(fā)小程序踩過(guò)的坑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
JavaScript惰性求值的一種實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于JavaScript惰性求值的一種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
JavaScript常見(jiàn)事件源與事件流的獲取方法及解析
這篇文章主要為大家介紹了JavaScript常見(jiàn)事件源與事件流的獲取方法及解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

