`
shuaigg.babysky
  • 浏览: 571065 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

结合prototype自己做的Sortable

 
阅读更多
<!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=utf-8" />
<script type="text/javascript">
try{document.execCommand("BackgroundImageCache", false, true);}catch(e){}
</script> 
<title>Sortable</title>
<script src="http://api.prototypejs.org/javascripts/pdoc/prototype.js" type="text/javascript" ></script>
<script src="http://script.aculo.us/scriptaculous.js" type="text/javascript" ></script>
</head>
<body>
<style>
#list li {height : 30px;border:1px solid black; width:200px;background-color:white;}
</style>
<ul id="list">
	<li node-type="sortitem">1</li>
	<li node-type="sortitem">2</li>
	<li node-type="sortitem">3</li>
	<li node-type="sortitem">4</li>
	<li node-type="sortitem">5</li>
	<li node-type="sortitem">6</li>
	<li node-type="sortitem">7</li>
	<li node-type="sortitem">8</li>
	<li node-type="sortitem">9</li>
	<li node-type="sortitem">10</li>
	<li node-type="sortitem">11</li>
	<li node-type="sortitem">12</li>
	<li node-type="sortitem">13</li>
	<li node-type="sortitem">14</li>
	<li node-type="sortitem">15</li>
	<li node-type="sortitem">16</li>
</ul>

<script>
(function($) {
	var parseDOM = function(dom) {
		var result = {};
		$(dom).select("[node-type]").each(function(item) {
			var key = item.readAttribute('node-type');
			if(!result[key]) {
				result[key] = item;
			} else {
				if(Object.isArray(result[key])) {
					result[key].push(item);
				} else {
					result[key] = [result[key]];
					result[key].push(item);
				}
			}
		});
		return result;
	};
	var sortable = function(dom) {
		Element.extend(dom);
		var nodes = parseDOM(dom);
		nodes.sortitem.each(function(item , i) {
			item.store('pos' , item.cumulativeOffset());
			item.store('height' , item.getHeight());
			item.setStyle('position:relative');
		});
		var mDownItem , mDownY , curItem;
		var findSortItem = function(e) {
			var item = e.findElement('[node-type="sortitem"]');
			if(item != document) {
				return item;
			}
			return null;
		};
		var getCurTarget = function(posY) {
			var target = null;
			nodes.sortitem.each(function(item) {
				if(item != mDownItem) {
					var pos = item.retrieve('pos');
					var height = item.retrieve('height');
					var start = pos.top;
					var end = start + height;
					if(start < posY && posY < end) {
						target = item;
						throw $break;
					}
				}
			});
			return target;
		};
		var getPointerX = function(e) {
			return e.pointerX();
		};
		var getPointerY = function(e) {
			return e.pointerY();
		};
		var mousedown = function(e) {
			e.stop();
			var item = findSortItem(e);
			if(!item) {
				return ;
			}
			mDownY = getPointerY(e);
			mDownItem = item;
			Element.extend(mDownItem);
			mDownItem.setStyle({zIndex : 10 , cursor : 'move'});
			if (mDownItem.setCapture !== undefined) {
				mDownItem.setCapture();
			}
			document.body.onselectstart = function(){return false;};
			dom.observe('mousemove' , mousemove);
			dom.observe('mouseup' , mouseup);
		};
		var IsGetThrough = function(targetItem , curItem , dir) {
			var targetItemPos = targetItem.cumulativeOffset();
			var curItemPos = curItem.retrieve("pos");
			var curItemHeight = curItem.retrieve("height");
			if(dir === 'down') {
				if((curItemHeight - curItemPos.top + targetItemPos.top) / curItemHeight > 0.66) {
					return true;
				} else {
					return false;
				}
			} else if(dir === 'up') {
				if(Math.abs(curItemPos.top - targetItemPos.top) / curItemHeight < 0.33) {
					return true;
				} else {
					return false;
				}
			}
		};
		var resetStorePos = function(t1 , t2) {
			t1.store('pos' , t1.cumulativeOffset());
			t2.store('pos' , t2.cumulativeOffset());
		};
		var mousemove = function(e) {
			if(!mDownItem) {
				return;
			}
			var currY = getPointerY(e);
			var y = currY - mDownY;
			if(!y) {
				return;
			}
			mDownItem.setStyle({
				top : y + 'px'
			});
			var dir = y > 0 ? 'down' : 'up';
			curItem = getCurTarget(currY);
			if(!curItem) {
				return;
			}
			Element.extend(curItem);
			var through = IsGetThrough(mDownItem , curItem , dir);
			if(through) {
				mDownY = getPointerY(e);
				mDownItem.setStyle({
					top : null
				});
				if(dir === 'down') {
					mDownItem.parentNode.insertBefore(curItem , mDownItem);
				} else if(dir === 'up') {
					mDownItem.parentNode.insertBefore(mDownItem , curItem);
				}
				resetStorePos(mDownItem , curItem);
			}
		};
		var mouseup = function() {
			if (mDownItem.releaseCapture !== undefined) {
				mDownItem.releaseCapture();
			}
			document.body.onselectstart = function(){return true;};
			mDownItem.setStyle({
				cursor : 'default'
			});
			mDownY = curItem = null;
			dom.stopObserving('mousemove' , mousemove);
			dom.stopObserving('mouseup' , mouseup);
			mDownItem.morph('top:0px' , {
				duration : 0.2,
				afterFinish : function() {
					mDownItem = null;
				}
			});
		};
		dom.observe('mousedown' , mousedown);
	};
	sortable($('list'));
})(window.$);
</script>

</body>
</html>
 
分享到:
评论

相关推荐

    prototype使用文档

    结合Prototype和Script.aculo.us,开发者可以构建具有高度交互性和视觉吸引力的Web应用程序。通过阅读提供的`Prototype.chm`和`Scriptaculous.chm`帮助文档,可以深入了解这两个库的详细用法和API,进一步提升...

    .prototype.and.scriptaculous.taking.the.pain.out.of.javascript

    6. **实战案例**:书中可能会包含一些实际项目示例,展示如何结合Prototype和Scriptaculous开发复杂的AJAX应用。 7. **兼容性和性能优化**:讨论如何确保代码在不同浏览器上的兼容性,以及如何优化AJAX请求以提高...

    jquery.fn.sortable.js:jQuery插件可排序

    在实际应用中,开发者可能需要结合其他jQuery插件或库,如`jQuery UI`的Sortable组件,以实现更复杂的功能,如连接多个可排序区域、处理拖放外部元素等。同时,了解如何调试和解决与`sortable.js`相关的布局问题也是...

    sortable-table:轻松创建带有可点击列标题的html表以对行进行排序

    总的来说,`sortable-table`结合CoffeeScript提供了高效且易于使用的表格排序解决方案。通过简单的配置和事件绑定,开发者可以快速实现交互式表格,提高数据展示的可用性和易用性。在实际项目中,这样的工具能大大...

    PracticalPrototypeAndScript.aculo.us

    2. **拖拽式文件上传**:结合Draggable和Droppable功能实现文件拖拽上传功能。 3. **动态面板切换**:使用Effects模块实现面板的平滑过渡效果。 4. **自定义控件**:基于Controlled Elements构建自定义的UI控件,如...

    scriptaculous-js-1.8.2教程与开发库

    总结来说,Scriptaculous是一个强大的JavaScript库,结合Prototype,能够帮助开发者实现丰富的交互效果,提升网站的用户体验。通过学习和实践,开发者可以轻松掌握这个工具,为自己的项目增添更多活力。

    bootstrap3_jquery_table_sortable

    这可能涉及到JavaScript的数组排序算法,如使用Array.prototype.sort()函数,并提供自定义比较函数以按照特定规则(升序、降序、按指定列等)排序。 5. **动态更新DOM**:当数据排序完成后,需要更新表格内容以反映...

    scriptaculous-js-1.7.1_beta3.zip

    **Scriptaculous.js框架详解** Scriptaculous.js是一款基于JavaScript的库,专为与Prototype...结合压缩包中的`scriptaculous-js-1.7.1_beta3`文件,你可以探索更多的示例和文档,进一步提升你的JavaScript开发技能。

    第11章 集成 Ajax1

    Script.aculo.us库与Prototype库结合使用,可以创造出更吸引人的界面。 此外,JSON(JavaScript Object Notation)在Ajax通信中扮演着重要角色,它是轻量级的数据交换格式,使得客户端和服务器之间的数据交换变得...

    21个新奇漂亮的AjaxCSS表格设计

    - **Sortable/Resizable/Editable Table Kit**:基于Prototype框架,支持表格的实时调整大小、排序和编辑。 - **Make all your tables sortable**:使所有表格可排序,提高数据管理效率。 - **Zebra Tables**:通过...

    类似浏览器首页拖动排序的js效果

    var newIndex = Array.prototype.indexOf.call(container, event.target); if (currentIndex ) { container.insertBefore(droppedElement, containerChildren[newIndex]); } else { container.insertBefore...

    【JavaScript源代码】elementui导出数据为xlsx、excel表格.docx

    在JavaScript开发中,ElementUI是一个基于Vue.js的组件库,提供了丰富的UI...总之,通过结合ElementUI、`file-saver` 和 `xlsx`,我们可以轻松地在Vue项目中实现数据的Excel导出,提供给用户更加便捷的数据管理方式。

    jQuery模块编辑器拖拽排序代码.zip

    jQuery插件通常遵循一种约定,即通过扩展$.fn(jQuery对象的prototype)来增加新的方法。在这个例子中,很可能是通过$.fn.draggable或$.fn.sortable等方法实现了拖放行为。这些方法可能包含初始化拖放事件、设置拖放...

    33种Javascript 表格排序控件收集

    2. Table sorting with Prototype(2):利用Prototype JavaScript框架实现表格排序,提供了一种简单的方法来对表格进行排序,支持多列排序。 3. Sorttable(3):由Kryogenix开发的一个轻量级解决方案,只需在表格...

    50个精彩JQuery插件案例

    1. **jQuery插件的原理**:jQuery插件是基于jQuery核心功能扩展的功能模块,通过$.fn.extend()方法,将新方法添加到jQuery对象的prototype上,使得所有jQuery选择器都能调用这些新方法。 2. **DOM操作**:jQuery...

Global site tag (gtag.js) - Google Analytics