jQuery插件之拖拽功能
2009-11-11 14:01
351 查看
$.fn.getCss=function(key) { var v = parseInt(this.css(key)); if (isNaN(v)) return false; return v; } $.fn.EasyDrag = function(opts) { opts = $.extend({ handler: null, //divHandler,即为移动区的句柄 onMove: function() { return false; }, onDrop: function() { return false; }, opacity: 0.7 }, opts || {}); var fndragData = { drag: function(e) { var dragData = e.data.dragData; dragData.target.css({ left: dragData.left + e.pageX - dragData.offLeft, top: dragData.top + e.pageY - dragData.offTop }); //移动div的实质是 鼠标move的点的x y 坐标 减去 鼠标down的点的xy左边,然后用目标div(或者其他容器)的left或者top加上这个坐标即可 dragData.handler.css({ cursor: 'move' }); dragData.onMove(e); }, drop: function(e) { var dragData = e.data.dragData; dragData.target.css(dragData.oldCss); //.css({ 'opacity': '' }); dragData.handler.css('cursor', dragData.oldCss.cursor); dragData.onDrop(e); $().unbind('mousemove', fndragData.drag) .unbind('mouseup', fndragData.drop).bind('selectstart', function() { return true; }); } }; return this.each(function() { var divhandler = null; var divmain = $(this); if (opts.handler == null || opts.handler == undefined) divhandler = $(this); else divhandler = $(opts.handler, this).length == 1 ? $(opts.handler, this) : $(this); divhandler.bind("mousedown", function(event) { var oldCss = {}; if (divmain.css('position') != 'absolute') { var e = { mouseClient: { x: event.clientX, y: event.clientY }, me: { h: divmain.position().top, w: divmain.position().left } }; divmain.css('position', 'absolute'); //alert(divmain.css("left")); try { divmain.css({ "left": e.me.w, "top": e.me.h }); } catch (ex) { } } oldCss.cursor = divmain.css('cursor') || 'default'; oldCss.opacity = 1; var dragData = { left: oldCss.left || divmain.getCss('left') || 0, //移动之前目标容器的left top: oldCss.top || divmain.getCss('top') || 0, //移动之前目标容器的top width: divmain.width() || divmain.getCss('width'), height: divmain.height() || divmain.getCss('height'), offLeft: event.pageX, //鼠标down的x(距离浏览器左边, 和 clientX 差不多) offTop: event.pageY, //鼠标down的y(浏览器右边, 和 clientY差不多) oldCss: oldCss, onMove: opts.onMove, onDrop: opts.onDrop, handler: divhandler, target: divmain } divmain.css('opacity', opts.opacity); $().bind('mousemove', { dragData: dragData }, fndragData.drag) .bind('mouseup', { dragData: dragData }, fndragData.drop).bind('selectstart', function() { return false; }); ; }); }); } $(document).ready(function() { $(".dragDiv").EasyDrag({ handler: ".handler" }); });
相关文章推荐
- 基于jquery插件实现拖拽删除图片功能
- jQuery插件实现文件上传功能(支持拖拽)
- 带裁剪的图片上传功能jquery插件
- jQuery应用-实现博客个性主页布局拖拽功能
- 在线Word编辑的jQuery插件时间:2010-12-29 09:15点击:122 次 【大 中 小】 在做OA或者工作流程的网站中,常常能够看到一些在线Word编辑进行文档处理的功能,这里我开发了
- 用html5页面引入了一个jquery插件,实现平板上的向上滑动加载更多的功能
- The jQuery HTML5 Audio / Video Library (jQuery jPlayer插件给你的站点增加视频和音频功能)
- jQuery Cycle Plugin插件 - 实现图片等元素循环展示功能
- jquery+ashx无刷新GridView数据显示插件(实现分页、排序、过滤功能)
- 带有查询和排序功能的jquery分页插件——DataTables
- jquery插件jquery.nicescroll实现图片无滚动条左右拖拽的方法
- jquery插件jquery.nicescroll实现图片无滚动条左右拖拽的方法
- jQuery使用zTree插件实现可拖拽的树示例
- JQuery插件第三十三个:高级搜索下的排序功能
- jquery插件开发之实现google+圈子选择功能
- MyEclipse6.0中使用aptana插件,添加jquery提示功能
- 基于jQuery实现类似Google+圈子选择功能插件(支持键盘事件)
- JQuery之拖拽插件
- ASP.NET jQuery 随笔 使用allValidator插件简单实现客户端验证功能