JavaScript實(shí)現(xiàn)網(wǎng)頁對(duì)象拖放功能的方法
本文實(shí)例講述了JavaScript實(shí)現(xiàn)網(wǎng)頁對(duì)象拖放功能的方法。分享給大家供大家參考。具體如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Drag</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style>
#myDiv{width:50px; height:50px; background-color:red}
</style>
</head>
<body>
<div id="myDiv"></div>
</body>
<script type="text/javascript">
var isIE = document.all ? true : false;
//判斷是否是IE瀏覽器
function addEventHandler(oTarget, sEventType, fnHandler){
//添加事件
if(oTarget.addEventListener){
oTarget.addEventListener(sEventType, fnHandler, false);
}else if(oTarget.attachEvent){
oTarget.attachEvent("on" + sEventType, fnHandler);
}else{
oTarget["on" + sEventType] = fnHandler;
}
}
function removeEventHandler(oTarget, sEventType, fnHandler) {
//移除事件
if (oTarget.removeEventListener) {
oTarget.removeEventListener(sEventType, fnHandler, false);
} else if (oTarget.detachEvent) {
oTarget.detachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = null;
}
}
var BindAsEventListener = function(object, fun) {
//以另一個(gè)對(duì)象替換當(dāng)前對(duì)象
return function(event) {
return fun.call(object, (event || window.event));
}
}
var $ = function(id){
return document.getElementById(id);
}
var Class = {
create: function() {
return function() {this.initialize.apply(this, arguments);}
}
}
var drag = Class.create();
drag.prototype = {
initialize: function(id){//初始化
this._drag = $(id);
this._flag = false;
addEventHandler(this._drag,'mousedown',BindAsEventListener(this,this.start));
this._fM = BindAsEventListener(this, this.move);
this._fS = BindAsEventListener(this, this.stop);
this._drag.style.position = "absolute";
},
start: function(oEvent){//相當(dāng)于onmousedown事件
//return this._name;
this._x = oEvent.clientX - this._drag.offsetLeft;
this._y = oEvent.clientY - this._drag.offsetTop;
addEventHandler(document, 'mousemove', this._fM);
addEventHandler(document, 'mouseup', this._fS);
if(isIE){
addEventHandler(this._drag, "losecapture", this._fS);
//焦點(diǎn)丟失
this._Handle.setCapture();//設(shè)置鼠標(biāo)捕獲
}else{
addEventHandler(window, "blur", this._fS);//焦點(diǎn)丟失
oEvent.preventDefault();//阻止默認(rèn)動(dòng)作
}
},
move: function(oEvent){ //相當(dāng)于onmonusemove事件
this._drag.style.left = oEvent.clientX - this._x + "px";
this._drag.style.top = oEvent.clientY - this._y + "px";
},
stop: function(){ //相當(dāng)于onmouseup事件
removeEventHandler(document, 'mousemove', this._fM);
removeEventHandler(document, 'mouseup', this._fS);
if(isIE){
removeEventHandler(this._drag, "losecapture", this._fS);
this._Handle.releaseCapture();
}else{
removeEventHandler(window, "blur", this._fS);
}
}
}
var ndrag = new drag("myDiv");
</script>
</html>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- Java實(shí)現(xiàn)鼠標(biāo)拖放功能的方法
- Vue.js實(shí)現(xiàn)拖放效果的實(shí)例
- javascript 拖放效果實(shí)現(xiàn)代碼
- JavaScript 拖放效果代碼
- Javascript拖拽&拖放系列文章3之細(xì)說事件對(duì)象
- JavaScript 圖片放大鏡(可拖放、縮放效果)
- 基于jQuery實(shí)現(xiàn)的百度導(dǎo)航li拖放排列效果,即時(shí)更新數(shù)據(jù)庫
- 廣泛收集的jQuery拖放插件集合
- asp.net+jquery Gridview的多行拖放, 以及跨控件拖放
- 腳本div實(shí)現(xiàn)拖放功能(兩種)
相關(guān)文章
JavaScript設(shè)計(jì)模式--橋梁模式引入操作實(shí)例分析
這篇文章主要介紹了JavaScript設(shè)計(jì)模式--橋梁模式,結(jié)合實(shí)例形式分析了JavaScript設(shè)計(jì)模式中橋梁模式應(yīng)用操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-05-05
javascript 驗(yàn)證碼生成代碼 推薦學(xué)習(xí)
非常不錯(cuò)的用javascript實(shí)現(xiàn)的驗(yàn)證碼實(shí)現(xiàn)代碼。2009-07-07
javascript substr和substring用法比較
在js中substring和substr都是用來截取字符串的,那么substring和substr之間的具體區(qū)別在哪里,有沒有區(qū)別呢,下面我來給各位詳細(xì)引用一些實(shí)例來介紹這些問題2009-06-06
bootstrap datepicker 與bootstrapValidator同時(shí)使用時(shí)選擇日期后無法正常觸發(fā)校驗(yàn)的解
這篇文章主要介紹了bootstrap datepicker 與bootstrapValidator同時(shí)使用時(shí)選擇日期后無法正常觸發(fā)校驗(yàn)的解決思路的相關(guān)資料,需要的朋友可以參考下2016-09-09
javascript 拽拉效果 供JS初學(xué)者學(xué)習(xí)參考
超精簡的拽拉效果JS,并且還是同時(shí)支持IE和FF。 可以給與其他剛開始學(xué)習(xí)JS的朋友作為參考和學(xué)習(xí)。2008-07-07
javascript實(shí)現(xiàn)全角與半角字符的轉(zhuǎn)換
這篇文章主要介紹了javascript實(shí)現(xiàn)全角與半角字符的轉(zhuǎn)換的相關(guān)代碼與知識(shí)點(diǎn)分享,需要的朋友可以參考下2015-01-01
Javascript實(shí)現(xiàn)html轉(zhuǎn)pdf高清版(提高分辨率)
這篇文章主要介紹了Javascript將html轉(zhuǎn)成pdf高清版(提高分辨率),需要的朋友可以參考下2020-02-02
echarts折線圖流動(dòng)特效的實(shí)現(xiàn)全過程(非平滑曲線)
最近因?yàn)楣緲I(yè)務(wù)需求,需要實(shí)現(xiàn),當(dāng)Echarts重新加載數(shù)據(jù)時(shí)實(shí)現(xiàn)動(dòng)態(tài)效果,下面這篇文章主要給大家介紹了關(guān)于echarts折線圖流動(dòng)特效實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-03-03

