亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

完美的js div拖拽實例代碼

 更新時間:2016年09月24日 15:13:37   作者:凱文加內特  
這篇文章主要為大家詳細介紹了完美的js div拖拽實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了完美的js div拖拽實例代碼,供大家參考,具體內容如下

<!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=GBK" />
<title>拖拽庫</title>
<style type="text/css">
div,h2,p{margin:0;padding:0;}
body{font:14px/1.5 arial;}
#box{width:100px;height:100px;background:#fef4eb;padding:5px;margin:50px;border:1px solid #f60;}
#box .title{height:25px;background:#f60;}
#tool{margin-bottom:10px;}
</style>
<script type="text/javascript">
function Drag()
{
 //初始化
 this.initialize.apply(this, arguments)
}
Drag.prototype = {
 //初始化
 initialize : function (drag, options)
 {
 this.drag = this.$(drag);
 this._x = this._y = 0;
 this._moveDrag = this.bind(this, this.moveDrag);
 this._stopDrag = this.bind(this, this.stopDrag);

 this.setOptions(options);

 this.handle = this.$(this.options.handle);
 this.maxContainer = this.$(this.options.maxContainer);

 this.maxTop = Math.max(this.maxContainer.clientHeight, this.maxContainer.scrollHeight) - this.drag.offsetHeight;
 this.maxLeft = Math.max(this.maxContainer.clientWidth, this.maxContainer.scrollWidth) - this.drag.offsetWidth;

 this.limit = this.options.limit;
 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.style.cursor = "move";

 this.changeLayout();

 this.addHandler(this.handle, "mousedown", this.bind(this, this.startDrag))
 },
 changeLayout : function ()
 {
 this.drag.style.top = this.drag.offsetTop + "px";
 this.drag.style.left = this.drag.offsetLeft + "px";
 this.drag.style.position = "absolute";
 this.drag.style.margin = "0"
 },
 startDrag : function (event)
 { 
 var event = event || window.event;

 this._x = event.clientX - this.drag.offsetLeft;
 this._y = event.clientY - this.drag.offsetTop;

 this.addHandler(document, "mousemove", this._moveDrag);
 this.addHandler(document, "mouseup", this._stopDrag);

 event.preventDefault && event.preventDefault();
 this.handle.setCapture && this.handle.setCapture();

 this.onStart()
 },
 moveDrag : function (event)
 {
 var event = event || window.event;

 var iTop = event.clientY - this._y;
 var iLeft = event.clientX - this._x;

 if (this.lock) return;

 this.limit && (iTop < 0 && (iTop = 0), iLeft < 0 && (iLeft = 0), iTop > this.maxTop && (iTop = this.maxTop), iLeft > this.maxLeft && (iLeft = this.maxLeft));

 this.lockY || (this.drag.style.top = iTop + "px");
 this.lockX || (this.drag.style.left = iLeft + "px");

 event.preventDefault && event.preventDefault();

 this.onMove()
 },
 stopDrag : function ()
 {
 this.removeHandler(document, "mousemove", this._moveDrag);
 this.removeHandler(document, "mouseup", this._stopDrag);

 this.handle.releaseCapture && this.handle.releaseCapture();

 this.onStop()
 },
 //參數(shù)設置
 setOptions : function (options)
 {
 this.options =
 {
  handle:  this.drag, //事件對象
  limit:  true, //鎖定范圍
  lock:  false, //鎖定位置
  lockX:  false, //鎖定水平位置
  lockY:  false, //鎖定垂直位置
  maxContainer: document.documentElement || document.body, //指定限制容器
  onStart: function () {}, //開始時回調函數(shù)
  onMove:  function () {}, //拖拽時回調函數(shù)
  onStop:  function () {} //停止時回調函數(shù)
 };
 for (var p in options) this.options[p] = options[p]
 },
 //獲取id
 $ : function (id)
 {
 return typeof id === "string" ? document.getElementById(id) : id
 },
 //添加綁定事件
 addHandler : function (oElement, sEventType, fnHandler)
 {
 return oElement.addEventListener ? oElement.addEventListener(sEventType, fnHandler, false) : oElement.attachEvent("on" + sEventType, fnHandler)
 },
 //刪除綁定事件
 removeHandler : function (oElement, sEventType, fnHandler)
 {
 return oElement.removeEventListener ? oElement.removeEventListener(sEventType, fnHandler, false) : oElement.detachEvent("on" + sEventType, fnHandler)
 },
 //綁定事件到對象
 bind : function (object, fnHandler)
 {
 return function ()
 {
  return fnHandler.apply(object, arguments) 
 }
 }
};
 
 
//應用
window.onload = function ()
{
 var oBox = document.getElementById("box"); 
 var oTitle = oBox.getElementsByTagName("h2")[0];
 var oSpan = document.getElementsByTagName("span")[0];
 var oDrag = new Drag(oBox, {handle:oTitle, limit:false});

 var aInput = document.getElementsByTagName("input");

 //鎖定范圍接口
 aInput[0].onclick = function ()
 {
 oDrag.limit = !oDrag.limit;
 this.value = oDrag.limit ? "取消鎖定范圍" : "鎖定范圍"
 };

 //水平鎖定接口
 aInput[1].onclick = function ()
 {
 oDrag.lockX = !oDrag.lockX;
 this.value = oDrag.lockX ? "取消水平鎖定" : "水平鎖定"
 };

 //垂直鎖定接口
 aInput[2].onclick = function ()
 {
 oDrag.lockY = !oDrag.lockY;
 this.value = oDrag.lockY ? "取消垂直鎖定" : "垂直鎖定"
 };

 //鎖定位置接口
 aInput[3].onclick = function ()
 {
 oDrag.lock = !oDrag.lock;
 this.value = oDrag.lock ? "取消鎖定位置" : "鎖定位置"
 };

 //開始拖拽時方法
 oDrag.onStart = function ()
 {
 oSpan.innerHTML = "開始拖拽" 
 };

 //開始拖拽時方法
 oDrag.onMove = function ()
 {
 oSpan.innerHTML = "left:" + this.drag.offsetLeft + ", top:" + this.drag.offsetTop 
 };

 //開始拖拽時方法
 oDrag.onStop = function ()
 {
 oSpan.innerHTML = "結束拖拽" 
 };
};
</script>
</head>
<body>
<div id="tool">
 <input type="button" value="鎖定范圍" />
  <input type="button" value="水平鎖定" />
  <input type="button" value="垂直鎖定" />
  <input type="button" value="鎖定位置" />
</div>
<p>拖放狀態(tài):<span>未開始</span></p>
<div id="box">
 <h2 class="title"></h2>
</div>
</body>
</html>
</td>
  </tr>
 </table>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • iSlider手機端圖片滑動切換插件使用詳解

    iSlider手機端圖片滑動切換插件使用詳解

    這篇文章主要為大家詳細介紹了iSlider手機端圖片滑動切換插件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • js項目中前端如何實現(xiàn)無感刷新token

    js項目中前端如何實現(xiàn)無感刷新token

    本文主要介紹了js項目中前端如何實現(xiàn)無感刷新token,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • webpack4 css打包壓縮問題的解決

    webpack4 css打包壓縮問題的解決

    本篇文章主要介紹了webpack4 css打包壓縮問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • TypeScript對于Duck類型和模塊命名空間應用

    TypeScript對于Duck類型和模塊命名空間應用

    這篇文章主要介紹了TypeScript對于Duck類型和模塊命名空間應用,Duck類型是一種動態(tài)類型和多態(tài)形式,在duck類型中,重點是對象的行為可以做什么,而不是對象所屬的類型
    2022-08-08
  • 阻止JavaScript事件冒泡傳遞(cancelBubble 、stopPropagation)

    阻止JavaScript事件冒泡傳遞(cancelBubble 、stopPropagation)

    阻止JavaScript事件冒泡傳遞(cancelBubble 、stopPropagation)...
    2007-05-05
  • JavaScript中async與await實現(xiàn)原理與細節(jié)

    JavaScript中async與await實現(xiàn)原理與細節(jié)

    這篇文章主要介紹了JavaScript中async與await實現(xiàn)原理與細節(jié),文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • JavaScript中關于iframe滾動條的去除和保留

    JavaScript中關于iframe滾動條的去除和保留

    在開發(fā)中經(jīng)常遇到去掉全部的滾動條,去掉右邊的滾動條且保留底下的滾動條,去掉底下的滾動條且保留右邊的滾動條,大家基于js是怎么實現(xiàn)的呢?下面小編通過本文給大家詳細介紹下,對js iframe滾動條相關知識感興趣的朋友一起學習吧
    2016-11-11
  • 小程序轉發(fā)探索示例

    小程序轉發(fā)探索示例

    這篇文章主要介紹了小程序轉發(fā)探索示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • Firefox中使用outerHTML的2種解決方法

    Firefox中使用outerHTML的2種解決方法

    這篇文章主要介紹了Firefox中使用outerHTML的2種解決方法,需要的朋友可以參考下
    2014-06-06
  • el-form實現(xiàn)表單和圖片手動上傳和校驗功能

    el-form實現(xiàn)表單和圖片手動上傳和校驗功能

    在寫項目時,難免遇到需要上傳表單,圖片等文件,且表單內容需進行驗證及必填項提示,圖片需要和信息一起傳遞且圖片載入后需可預覽,這篇文章給大家介紹el-form實現(xiàn)表單和圖片手動上傳和校驗功能,感興趣的朋友一起看看吧
    2024-01-01

最新評論