jQuery 版元素拖拽原型代碼
更新時間:2011年04月25日 01:17:42 作者:
“元素拖拽”顧名思義:拖動,移動,拽放。有很多的解釋,而且在網(wǎng)上相關資料非常之多,本人在開發(fā) AsyncBox 當中也用到此效果,借此分享,同時也是一種技術積累。
本文主要針對拖拽原型進行解析,給剛接觸 JQuery 的愛好者一個簡單的示例。
在引入 Jquery.js 后:
<script type="text/javascript">
$(function(){
//綁定拖動元素對象
bindDrag(document.getElementById('test'));
});
function bindDrag(el){
//初始化參數(shù)
var els = el.style,
//鼠標的 X 和 Y 軸坐標
x = y = 0;
//邪惡的食指
$(el).mousedown(function(e){
//按下元素后,計算當前鼠標位置
x = e.clientX - el.offsetLeft;
y = e.clientY - el.offsetTop;
//IE下捕捉焦點
el.setCapture && el.setCapture();
//綁定事件
$(document).bind('mousemove',mouseMove).bind('mouseup',mouseUp);
});
//移動事件
function mouseMove(e){
//宇宙超級無敵運算中...
els.top = e.clientY - y + 'px';
els.left = e.clientX - x + 'px';
}
//停止事件
function mouseUp(){
//IE下釋放焦點
el.releaseCapture && el.releaseCapture();
//卸載事件
$(document).unbind('mousemove',mouseMove).unbind('mouseup',mouseUp);
}
}
</script>
下載:完整示例
在引入 Jquery.js 后:
復制代碼 代碼如下:
<script type="text/javascript">
$(function(){
//綁定拖動元素對象
bindDrag(document.getElementById('test'));
});
function bindDrag(el){
//初始化參數(shù)
var els = el.style,
//鼠標的 X 和 Y 軸坐標
x = y = 0;
//邪惡的食指
$(el).mousedown(function(e){
//按下元素后,計算當前鼠標位置
x = e.clientX - el.offsetLeft;
y = e.clientY - el.offsetTop;
//IE下捕捉焦點
el.setCapture && el.setCapture();
//綁定事件
$(document).bind('mousemove',mouseMove).bind('mouseup',mouseUp);
});
//移動事件
function mouseMove(e){
//宇宙超級無敵運算中...
els.top = e.clientY - y + 'px';
els.left = e.clientX - x + 'px';
}
//停止事件
function mouseUp(){
//IE下釋放焦點
el.releaseCapture && el.releaseCapture();
//卸載事件
$(document).unbind('mousemove',mouseMove).unbind('mouseup',mouseUp);
}
}
</script>
下載:完整示例
您可能感興趣的文章:
相關文章
jQuery是用來干什么的 jquery其實就是一個js框架
jQuery是一bai個簡潔而快速的JavaScript庫,可用于du簡化zhi事件處理,HTML文檔遍歷,Ajax交互和dao動畫,以更快速開發(fā)網(wǎng)站2021-02-02jQuery中操控hidden、disable等無值屬性的方法
在實現(xiàn)一些表單提交頁面時會放置若干隱藏屬性,下面為大家介紹下jQuery中如何操控hidden、disable等無值屬性,感興趣的朋友不要錯過2014-01-01jquery中cookie用法實例詳解(獲取,存儲,刪除等)
這篇文章主要介紹了jquery中cookie用法,結合實例詳細分析了jQuery操作cookie的獲取,存儲,刪除等操作,并附帶了Jquery操作Cookie記錄用戶查詢過信息實現(xiàn)方法,需要的朋友可以參考下2016-01-01