jquery 实现拖动的插件,试了几个,interface 是一个,但是 bug 太多,代码又复杂不好修改,就放弃了。
今天找到了一个叫做 easydrag 的,但用了下也不灵活,于是扩充了一下,现在可以指定拖动的 handle.
jquery.easydrag.js(以下绿色部分是我增加或修改的代码):
/**
* EasyDrag 1.3 - Drag & Drop jQuery Plug-in
*
* For usage instructions please visit http://fromvega.com
*
* Copyright (c) 2007 fromvega
*/
(function($){
// to track if the mouse button is pressed
var isMouseDown = false;
// to track the current element being dragged
var currentElement = null;
// callback holders
var dropCallbacks = {};
var dragCallbacks = {};
// global position records
var lastMouseX;
var lastMouseY;
var lastElemTop;
var lastElemLeft;
// returns the mouse (cursor) current position
$.getMousePosition = function(e){
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
return { 'x': posx, 'y': posy };
}
// updates the position of the current element being dragged
$.updatePosition = function(e) {
var pos = $.getMousePosition(e);
var spanX = (pos.x - lastMouseX);
var spanY = (pos.y - lastMouseY);
$(currentElement).css("top", (lastElemTop + spanY));
$(currentElement).css("left", (lastElemLeft + spanX));
}
// when the mouse is moved while the mouse button is pressed
$(document).mousemove(function(e){
if(isMouseDown){
// update the position and call the registered function
$.updatePosition(e);
if(dragCallbacks[currentElement.id] != undefined){
dragCallbacks[currentElement.id](e);
}
return false;
}
});
// when the mouse button is released
$(document).mouseup(function(e){
if(isMouseDown){
isMouseDown = false;
if(dropCallbacks[currentElement.id] != undefined){
dropCallbacks[currentElement.id](e);
}
return false;
}
});
// register the function to be called while an element is being dragged
$.fn.ondrag = function(callback){
return this.each(function(){
dragCallbacks[this.id] = callback;
});
}
// register the function to be called when an element is dropped
$.fn.ondrop = function(callback){
return this.each(function(){
dropCallbacks[this.id] = callback;
});
}
// set an element as draggable - allowBubbling enables/disables event bubbling
$.fn.easydrag = function(allowBubbling, handle_ids){
return this.each(function(){
// if no id is defined assign a unique one
if(undefined == this.id) this.id = 'easydrag'+time();
if (handle_ids) {
// 修改鼠标光标为移动的形状
for (var i=0; i<handle_ids.length; i++) {
$("#" + handle_ids[i]).css("cursor", "move");
}
} else {
$(this).css("cursor", "move");
}
// when an element receives a mouse press
$(this).mousedown(function(e){
if (handle_ids) {
// 判断是否是在拖动某个 handle
var srcElement;
if (e)
srcElement = e.srcElement;
else
srcElement = window.event.srcElement;
var exists = false;
if (srcElement.id != undefined) {
for (var i=0; i<handle_ids.length; i++) {
if (handle_ids[i] == srcElement.id) {
exists = true;
break;
}
}
}
if (!exists)
return false;
}
// set it as absolute positioned
$(this).css("position", "absolute");
// set z-index
$(this).css("z-index", "10000");
// update track variables
isMouseDown = true;
currentElement = this;
// retrieve positioning properties
var pos = $.getMousePosition(e);
lastMouseX = pos.x;
lastMouseY = pos.y;
lastElemTop = this.offsetTop;
lastElemLeft = this.offsetLeft;
$.updatePosition(e);
return allowBubbling ? true : false;
});
});
}
})(jQuery);
win.easydrag(false, ['oDragHandle_' + id]);
// 自定义的拖放后处理,记录目标移动到的新位置
win.ondrop(function(){
// save position info
notesData[id].x = parseInt(win.css("left"));
notesData[id].y = parseInt(win.css("top"));
});
原文地址:http://www.cnblogs.com/RChen/archive/2007/09/20/easydrag.html
分享到:
相关推荐
jQuery拖拽插件drag.js
jquery.event.drag jquery拖动插件
jQuery ui drag插件实现浮动div拖动排列布局代码,jQuery实现拖动布局并将排序结果保存到数据库
jquery drag插件, 拖放插件(Drag and Drop) UI/Draggables. EasyDrag jQuery Plugin. jQuery Portlets. jqDnR - drag, drop resize. Drag Demos.
表格拖拽排序插件 Table Drag and Drop JQuery plugin v0.7 最新0.7版本
jQuery实现拖拽DIV放大缩小插件“idrag”
Dragscroll 是一款极小的拖拽风格的 JavaScript 库(978 字节)。它可以按住鼠标按钮实现滚动。 标签:dragscroll
bootstrap-table拖拽需要dragtable.css,jquery.dragtable.js,jquery-ui.js,bootstrap-table-reorder-columns.js
jquery_drag.rar jquery_drag.rar jquery_drag.rar jquery_drag.rar
兼容PC和移动端拖拽滚动jQuery插件
jQuery.dragmove 是 DOM 元素拖拽扩展插件。 支持的浏览器: – Google Chrome– Safari ( Desktop and Mobile )– Internet Explorer ( 9, 10, 11 )– Firefox 标签:jQuery
拖拽插件、drag 拖拽插件、drag 拖拽插件、drag 拖拽插件、drag
jquery.event.drag-2.0.rar
代码如下: <html xmlns=”http://www.w3.org/1999/xhtml”><head> <meta http-equiv=”Content-... charset=utf-8″> <title>jQuery Dynamic Drag’n Drop</title> [removed]</scr
很好用的jQuery拖拽排序插件,兼容IE浏览器
用jquery 寫的php 购物车 有拖动(drag n drop)。
jquery实现Drag and Drop,图片拖拽实例
HTML5 Sortable 是一个 jQuery 插件,使用本地拖拽 API 来创建可排序的列表和网格。 特性: 小于 1KB (简化和压缩 ) 使用本地的拖拽 API 支持列表和网格风格的布局 类似 jQuery-UI 可排序插件...
本文要介绍的是一款基于jQuery的图片列表拖拽排序布局插件,我们可以拖拽图片,将图片插入到其他任意图片的前面或者后面,实现图片重新布局的功能,这个功能可以让用户自定义桌面布局,也是jQuery拖拽插件的一个经典...