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)容感興趣的讀者可查看本站專題:《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)文章
基于JavaScript實(shí)現(xiàn)交互式博客
在Web開(kāi)發(fā)中,JavaScript(JS)是一種至關(guān)重要的編程語(yǔ)言,它使網(wǎng)頁(yè)具有交互性,本文主要介紹了如何使用JavaScript實(shí)現(xiàn)一個(gè)交互式博客,需要的可以了解下2024-01-01
Jquery Autocomplete 結(jié)合asp.net使用要點(diǎn)
Jquery的Autocomplete是一個(gè)很好的智能提示插件,但是在實(shí)際使用過(guò)程中還是會(huì)遇到一些小問(wèn)題.2010-10-10
js string 轉(zhuǎn) int 注意的問(wèn)題小結(jié)
Javascript將string類型轉(zhuǎn)換int類型的過(guò)程中總會(huì)出現(xiàn)不如意的問(wèn)題,下面為大家介紹下js string轉(zhuǎn)int的一些注意的問(wèn)題,感興趣的朋友可以參考下2013-08-08
javascript使用正則表達(dá)式實(shí)現(xiàn)注冊(cè)登入校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了javascript使用正則表達(dá)式實(shí)現(xiàn)注冊(cè)登入校驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09

