最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript實現(xiàn)的多種鼠標拖放效果

 更新時間:2015年11月03日 11:30:26   作者:企鵝  
這篇文章主要介紹了JavaScript實現(xiàn)的多種鼠標拖放效果,涉及JavaScript響應鼠標事件動態(tài)變換頁面元素屬性的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了JavaScript實現(xiàn)的多種鼠標拖放效果。分享給大家供大家參考,具體如下:

這是一款JavaScript鼠標拖放效果代碼,通過本示例了解觸發(fā)對象,設置范圍限制,指定容器大小水平及垂直鎖定,還包括獲取和釋放焦點等。

運行效果截圖如下:

在線演示地址如下:

http://demo.jb51.net/js/2015/js-mouse-move-fix-style-codes/

具體代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>拖放效果</title>
</head>
<body>
<script> 
var isIE = (document.all) ? true : false;
var $ = function (id) {
 return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
 create: function() {
  return function() { this.initialize.apply(this, arguments); }
 }
}
var Extend = function(destination, source) {
 for (var property in source) {
  destination[property] = source[property];
 }
}
var Bind = function(object, fun) {
 return function() {
  return fun.apply(object, arguments);
 }
}
var BindAsEventListener = function(object, fun) {
 return function(event) {
  return fun.call(object, (event || window.event));
 }
}
var CurrentStyle = function(element){
 return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}
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 Drag = Class.create();
Drag.prototype = {
//拖放對象
 initialize: function(drag, options) {
 this.Drag = $(drag);//拖放對象
 this._x = this._y = 0;//記錄鼠標相對拖放對象的位置
 this._marginLeft = this._marginTop = 0;//記錄margin
 //事件對象(用于綁定移除事件)
 this._fM = BindAsEventListener(this, this.Move);
 this._fS = Bind(this, this.Stop);
 this.SetOptions(options);
 this.Limit = !!this.options.Limit;
 this.mxLeft = parseInt(this.options.mxLeft);
 this.mxRight = parseInt(this.options.mxRight);
 this.mxTop = parseInt(this.options.mxTop);
 this.mxBottom = parseInt(this.options.mxBottom);
 this.LockX = !!this.options.LockX;
 this.LockY = !!this.options.LockY;
 this.Lock = !!this.options.Lock;
 this.onStart = this.options.onStart;
 this.onMove = this.options.onMove;
 this.onStop = this.options.onStop;
 this._Handle = $(this.options.Handle) || this.Drag;
 this._mxContainer = $(this.options.mxContainer) || null;
 this.Drag.style.position = "absolute";
 //透明
 if(isIE && !!this.options.Transparent){
  //填充拖放對象
  with(this._Handle.appendChild(document.createElement("div")).style){
   width = height = "100%"; backgroundColor = "#fff"; filter = "alpha(opacity:0)";
  }
 }
 //修正范圍
 this.Repair();
 addEventHandler(this._Handle, "mousedown", BindAsEventListener(this, this.Start));
 },
 //設置默認屬性
 SetOptions: function(options) {
 this.options = {//默認值
  Handle:   "",//設置觸發(fā)對象(不設置則使用拖放對象)
  Limit:   false,//是否設置范圍限制(為true時下面參數(shù)有用,可以是負數(shù))
  mxLeft:   0,//左邊限制
  mxRight:  9999,//右邊限制
  mxTop:   0,//上邊限制
  mxBottom:  9999,//下邊限制
  mxContainer: "",//指定限制在容器內
  LockX:   false,//是否鎖定水平方向拖放
  LockY:   false,//是否鎖定垂直方向拖放
  Lock:   false,//是否鎖定
  Transparent: false,//是否透明
  onStart:  function(){},//開始移動時執(zhí)行
  onMove:   function(){},//移動時執(zhí)行
  onStop:   function(){}//結束移動時執(zhí)行
 };
 Extend(this.options, options || {});
 },
 //準備拖動
 Start: function(oEvent) {
 if(this.Lock){ return; }
 this.Repair();
 //記錄鼠標相對拖放對象的位置
 this._x = oEvent.clientX - this.Drag.offsetLeft;
 this._y = oEvent.clientY - this.Drag.offsetTop;
 //記錄margin
 this._marginLeft = parseInt(CurrentStyle(this.Drag).marginLeft) || 0;
 this._marginTop = parseInt(CurrentStyle(this.Drag).marginTop) || 0;
 //mousemove時移動 mouseup時停止
 addEventHandler(document, "mousemove", this._fM);
 addEventHandler(document, "mouseup", this._fS);
 if(isIE){
  //焦點丟失
  addEventHandler(this._Handle, "losecapture", this._fS);
  //設置鼠標捕獲
  this._Handle.setCapture();
 }else{
  //焦點丟失
  addEventHandler(window, "blur", this._fS);
  //阻止默認動作
  oEvent.preventDefault();
 };
 //附加程序
 this.onStart();
 },
 //修正范圍
 Repair: function() {
 if(this.Limit){
  //修正錯誤范圍參數(shù)
  this.mxRight = Math.max(this.mxRight, this.mxLeft + this.Drag.offsetWidth);
  this.mxBottom = Math.max(this.mxBottom, this.mxTop + this.Drag.offsetHeight);
  //如果有容器必須設置position為relative來相對定位,并在獲取offset之前設置
  !this._mxContainer || CurrentStyle(this._mxContainer).position == "relative" || (this._mxContainer.style.position = "relative");
 }
 },
 //拖動
 Move: function(oEvent) {
 //判斷是否鎖定
 if(this.Lock){ this.Stop(); return; };
 //清除選擇
 window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
 //設置移動參數(shù)
 var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
 //設置范圍限制
 if(this.Limit){
  //設置范圍參數(shù)
  var mxLeft = this.mxLeft, mxRight = this.mxRight, mxTop = this.mxTop, mxBottom = this.mxBottom;
  //如果設置了容器,再修正范圍參數(shù)
  if(!!this._mxContainer){
   mxLeft = Math.max(mxLeft, 0);
   mxTop = Math.max(mxTop, 0);
   mxRight = Math.min(mxRight, this._mxContainer.clientWidth);
   mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);
  };
  //修正移動參數(shù)
  iLeft = Math.max(Math.min(iLeft, mxRight - this.Drag.offsetWidth), mxLeft);
  iTop = Math.max(Math.min(iTop, mxBottom - this.Drag.offsetHeight), mxTop);
 }
 //設置位置,并修正margin
 if(!this.LockX){ this.Drag.style.left = iLeft - this._marginLeft + "px"; }
 if(!this.LockY){ this.Drag.style.top = iTop - this._marginTop + "px"; }
 //附加程序
 this.onMove();
 },
 //停止拖動
 Stop: function() {
 //移除事件
 removeEventHandler(document, "mousemove", this._fM);
 removeEventHandler(document, "mouseup", this._fS);
 if(isIE){
  removeEventHandler(this._Handle, "losecapture", this._fS);
  this._Handle.releaseCapture();
 }else{
  removeEventHandler(window, "blur", this._fS);
 };
 //附加程序
 this.onStop();
 }
};
</script>
<style> 
#idContainer{ border:10px solid #990000; width:600px; height:300px;}
#idDrag{ border:5px solid #C4E3FD; background:#C4E3FD; width:50px; height:50px; top:50px; left:50px;}
#idHandle{cursor:move; height:25px; background:#0000FF; overflow:hidden;}
</style>
<div id="idContainer">
<div id="idDrag"><div id="idHandle"></div></div>
</div>
<input id="idReset" type="button" value="復位" />
<input id="idLock" type="button" value="鎖定" />
<input id="idLockX" type="button" value="鎖定水平" />
<input id="idLockY" type="button" value="鎖定垂直" />
<input id="idLimit" type="button" value="范圍鎖定" />
<input id="idLimitOff" type="button" value="取消范圍鎖定" />
<br />拖放狀態(tài):<span id="idShow">未開始</span>
<script> 
var drag = new Drag("idDrag", { mxContainer: "idContainer", Handle: "idHandle", Limit: true,
 onStart: function(){ $("idShow").innerHTML = "開始拖放"; },
 onMove: function(){ $("idShow").innerHTML = "left:"+this.Drag.offsetLeft+";top:"+this.Drag.offsetTop; },
 onStop: function(){ $("idShow").innerHTML = "結束拖放"; }
});
$("idReset").onclick = function(){
 drag.Limit = true;
 drag.mxLeft = drag.mxTop = 0;
 drag.mxRight = drag.mxBottom = 9999;
 drag.LockX = drag.LockY = drag.Lock = false;
}
$("idLock").onclick = function(){ drag.Lock = true; }
$("idLockX").onclick = function(){ drag.LockX = true; }
$("idLockY").onclick = function(){ drag.LockY = true; }
$("idLimit").onclick = function(){  drag.mxRight = drag.mxBottom = 200;drag.Limit = true; }
$("idLimitOff").onclick = function(){ drag.Limit = false; }
</script>
</body>
</html>

希望本文所述對大家JavaScript程序設計有所幫助。

相關文章

  • 使用typescript+webpack構建一個js庫的示例詳解

    使用typescript+webpack構建一個js庫的示例詳解

    這篇文章主要介紹了typescript+webpack構建一個js庫,本文主要記錄使用typescript配合webpack打包一個javascript library的配置過程,需要的朋友可以參考下
    2022-07-07
  • 純js實現(xiàn)仿QQ郵箱彈出確認框

    純js實現(xiàn)仿QQ郵箱彈出確認框

    仿QQ郵箱的彈出層,彈出確認框,主要是用火狐的firebug把html和css扣了下來,沒有做封裝,就定義了一個拖動事件. 大家可以封裝自己的彈出窗,嘿嘿!
    2015-04-04
  • replace()方法查找字符使用示例

    replace()方法查找字符使用示例

    查找字符的情況下會使用replace()方法,此方法很常見也很實用,下面有個不錯的示例,感興趣的朋友可以參考下,希望對大家有所把幫助
    2013-10-10
  • Bootstrap3.0學習教程之JS折疊插件

    Bootstrap3.0學習教程之JS折疊插件

    這篇文章主要介紹了Bootstrap3.0學習教程之JS折疊插件的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-05-05
  • 不刷新網(wǎng)頁就能鏈接新的js文件方法總結

    不刷新網(wǎng)頁就能鏈接新的js文件方法總結

    在本篇文章里小編給大家整理的是關于不刷新網(wǎng)頁就能鏈接新的js文件方法總結,需要的朋友們參考下。
    2020-03-03
  • TypeScript中的interface與type實戰(zhàn)

    TypeScript中的interface與type實戰(zhàn)

    這篇文章主要介紹了TypeScript中的interface與type詳解,它們都是用來定義類型的強大工具,在實際開發(fā)中,你可以根據(jù)具體情況選擇使用 interface 或 type,或者甚至將它們結合起來使用,需要的朋友可以參考下
    2023-06-06
  • 跟我學習javascript的call(),apply(),bind()與回調

    跟我學習javascript的call(),apply(),bind()與回調

    跟我學習javascript的call(),apply(),bind()與回調,感興趣的小伙伴們可以參考一下
    2015-11-11
  • JavaScript面向對象的實現(xiàn)方法小結

    JavaScript面向對象的實現(xiàn)方法小結

    這篇文章主要介紹了JavaScript面向對象的實現(xiàn)方法,實例總結了兩種常見的面向對象的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • Javascript實現(xiàn)跨域后臺設置攔截的方法詳解

    Javascript實現(xiàn)跨域后臺設置攔截的方法詳解

    這篇文章主要給大家介紹了關于Javascript實現(xiàn)跨域后臺設置攔截的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。
    2017-08-08
  • JavaScript中常用的3種彈窗(警告框、確認框、提示框)

    JavaScript中常用的3種彈窗(警告框、確認框、提示框)

    JavaScript提供了幾種常用的彈窗方法,用于與用戶進行交互或顯示提示消息,這篇文章主要給大家介紹了關于JavaScript中常用的3種彈窗的相關資料,分別包括警告框、確認框、提示框,需要的朋友可以參考下
    2023-09-09

最新評論

应用必备| 扶余县| 安化县| 大荔县| 墨玉县| 福州市| 仙居县| 桂东县| 襄城县| 西充县| 仪陇县| 新建县| 通城县| 灌阳县| 东至县| 武安市| 洛扎县| 通渭县| 观塘区| 靖边县| 乌鲁木齐市| 高邑县| 长葛市| 庐江县| 饶河县| 清河县| 象州县| 读书| 娄烦县| 若尔盖县| 渭源县| 绥中县| 综艺| 遂昌县| 沙田区| 衡阳县| 崇明县| 墨江| 万源市| 云霄县| 綦江县|