js 拖动排序功能组件
2014-04-18 19:31
274 查看
1.基于jquery
2.效率优先
3.可配置项(拖动者,鼠标样式)
欢迎PK效率!!
2.效率优先
3.可配置项(拖动者,鼠标样式)
欢迎PK效率!!
/** * dragsort.js * * @example * new DH.Widget.Dragsort({ * el : 'string' // 拖动容器, 必填项 * dragSelector : 'string', // 拖动元素选择器, 非必填项 * moveSelector : 'string', // 被拖动元素选择器, 非必填项 * moveClassName : 'string', // 被多动元素类 * placeHolder : 'string', // 占位符元素, 如: <div>, 非必填项 * cursor : 'string', // 拖动鼠标样式, 非必填项 * zIndex : number, // 被拖动元素z-index, 非必填项, 默认999 * opacity : number // 被拖动元素透明度, 非必填项, 默认 0.5 * }) * */ (function($){ var Dragsort = DH.Base.create({ init : function(){ if(!this.el) return; var firstChild = this.el.children().get(0).nodeName.toLowerCase(); this.dragSelector = this.options.dragSelector || firstChild; this.moveSelector = this.options.moveSelector || firstChild; this.moveClassName = this.options.moveClassName; this.$place = this.options.placeHolder && $(this.options.placeHolder) || $('<' + firstChild + '>'); this.cursor = this.options.cursor || 'move'; this.zIndex = this.options.zIndex || 999; this.opacity = this.options.opacity || 0.5; this.dragFn = this.proxy(this.drag); this.el.delegate(this.dragSelector, 'mousedown', this.dragFn); }, drag : function(e){ var $e = $(e.currentTarget), $drag = this.$drag = $e, $move = this.$move = $e.closest(this.moveSelector + ':visible'), $place = this.$place, pageX = e.pageX, pageY = e.pageY, position = $move.position(), offset = $move.offset(), left = position.left, top = position.top ; $drag .css({ cursor : this.cursor }) ; $move.find('td').length && $move.find('td').each(function(){ $(this).width($(this).width()); }); $move .css({ position : 'absolute', left : left, top : top, width : $move.width(), height : $move.height(), zIndex : this.zIndex, opacity : this.opacity, '-moz-user-select' : 'none' // stop moz text select }) .after($place) ; this.moveClassName && $move.addClass(this.moveClassName); $place .show() .css({ width : $move.width(), height : $move.height() }) .addClass('dragsort-placeholder') ; this.start = { left : pageX - left, top : pageY - top } this.sortFn = this.proxy(this.sort); this.stopFn = this.proxy(this.stop); $(document) .bind('mousemove', this.sortFn) .bind('mouseup', this.stopFn) .bind('selectstart', this.stopBubble) // stop ie text select ; this.canDrag = true; return false; // 阻止拖动元素事件冒泡 }, sort : function(e){ if(!this.canDrag) return; var $move = this.$move, $place = this.$place, pageX = e.pageX, pageY = e.pageY, start = this.start, left = pageX - start.left, top = pageY - start.top, position = $move.position(), hw = $move.width() / 2, hh = $move.height() / 2, $prevs = $move.prevAll().not(':hidden'), $nexts = $move.nextAll().not('.dragsort-placeholder').not(':hidden') ; $move.css({ left : left, top : top }); $prevs.length && $prevs.each(function(){ var $this = $(this), tposition = $this.position() ; if(position.left - hw < tposition.left && position.top - hh < tposition.top){ $place.after($this); $place.before($move); } }); $nexts.length && $nexts.each(function(){ var $this = $(this), tposition = $this.position() ; if(position.left + hw > tposition.left && position.top + hh > tposition.top){ $this.after($place); $place.before($move); } }); }, stop : function(){ var $drag = this.$drag, $move = this.$move, $place = this.$place ; this.canDrag = false; $(document) .unbind('mousemove', this.sortFn) .unbind('mouseup', this.stopFn) .unbind('selectstart', this.stopBubble) ; this.sortFn = null; this.stopFn = null; $drag .css({ cursor : '' }) ; $move.find('td').length && $move.find('td').each(function(){ $(this).width(''); $(this).attr('style') === '' && $(this).removeAttr('style'); }); $move .css({ position : '', left : '', top : '', width : '', height : '', zIndex : '', opacity : '', '-moz-user-select' : '' }) ; this.moveClassName && $move.removeClass(this.moveClassName); $move.attr('style') === '' && $move.removeAttr('style'); $move.attr('class') === '' && $move.removeAttr('class'); $place.after($move).hide(); this.trigger('stop'); }, stopBubble: function(){ return false; }, once : function(){ this.bind('stop', this.proxy(this.destory)); this.trigger('once'); }, destory : function(){ this.el.undelegate(this.dragSelector, 'mousedown', this.dragFn); this.dragFn = null; this.el = null; this.trigger('destory'); this.$place.remove(); this.$place = null; this.$drag = null; this.$move = null; } }); DH.Widget.Dragsort = Dragsort; })(window.jQuery);
相关文章推荐
- js 拖动功能组件
- 基于JS组件实现拖动滑块验证功能(代码分享)
- js功能类库放送(一) 拖动功能库
- 基于BootStrap的Metronic框架实现页面链接收藏夹功能按钮移动收藏记录(使用Sortable进行拖动排序)
- DHTMLX中对dhtmlxgrid表格行的置顶置底功能,以及拖动排序功能的实现
- Vue 实现拖动滑块验证功能(只有css+js没有后台验证步骤)
- JS实现的点击表头排序功能示例
- JS组件系列——Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案
- Vue.js实现多条件筛选、搜索、排序及分页的表格功能
- Vue.js实现多条件筛选、搜索、排序及分页的表格功能
- DHTMLX中对dhtmlxgrid表格行的置顶置底操作,以及拖动排序功能的实现
- JS 页面列表自由拖动功能。 IE内核下兼容。。不兼容谷歌。。
- JS+CSS制作的DIV层最小化和随意拖拽排序功能
- js 处理form表单整理成数组,结合php 完成排序功能的实现
- Android基于widget组件实现物体移动/控件拖动功能示例
- iOS UITableView的细致功能(滑动删除,拖动排序,添加行,搜索过滤)
- 基于BootStrap的Metronic框架实现页面链接收藏夹功能按钮移动收藏记录(使用Sortable进行拖动排序)
- js 静态HTML表格排序功能实现
- js拖动功能库
- 用 Jquery 的UI DRAG组件实现拖动功能