js metro仿win8卡片效果

原創地址:http://blog.csdn.net/jlq_diligence/article/details/12718177

Metro風格設計主要特點

1.Windows8Metro風格設計,實現網站或系統功能的導航

2.Javascript實現

3.支持所有IE360Chrome等常用瀏覽器

4.支持圓角、陰影、卡片切換等特效

5.支持卡片的放大、縮小、增加、刪除等功能

6.可自定義卡片背景色、背景圖片、卡片圖片、卡片文字

7.卡片間可任意切換

Metro風格截圖

SouthEast

Metro部分算法

卡片顯示部分代碼:

  1. str='<lidata-mode="flip"id="'+itemID+'"data-sizex="1"data-sizey="1"class="stylediv2"style="float:left;left:'+left+'px;top:'+top+'px;width:'+addwidth+'px;height:'+addheight+'px;background-color:'+backColor+';position:absolute;z-index:2;cursor:pointer;border-radius:5px;box-shadow:04px10pxrgba(0,0,0,.35);behavior:url('+imgBasePath+'js/ie.htc);"onMouseOver="showOption(this)"onMouseOut="hiddeOption(this)">';

  2. str+='<divstyle="float:left;position:absolute;left:0px;top:0px;width:'+addwidth+'px;height:'+addheight+'px;background-color:orange;"></div>';

  3. str+='<divstyle="float:left;position:absolute;left:0px;top:0px;width:'+addwidth+'px;height:'+addheight+'px;background-color:red;"></div>';

  4. str+='</li>';

str='<li data-mode="flip" id="'+itemID+'" data-sizex="1" data-sizey="1" class="stylediv2" style="float:left;left:'+left+'px;top:'+top+'px;width: '+addwidth+'px; height: '+addheight+'px;background-color:'+backColor+';position:absolute;z-index:2; cursor:pointer;border-radius:5px;box-shadow:0 4px 10px rgba(0, 0, 0, .35);  url('+imgBasePath+'js /ie.htc);" onMouseOver="showOption(this)" onMouseOut="hiddeOption(this)">';
str+='<div style="float:left;position:absolute;left:0px;top:0px;width: '+addwidth+'px; height: '+addheight+'px;background-color:orange;"></div>';
str+='<div style="float:left;position:absolute;left:0px;top:0px;width: '+addwidth+'px; height: '+addheight+'px;background-color:red;"></div>';
str+='</li>';


移動單元格JS腳本:

  1. functionmoveItem(objid,indexList){

  2. //判斷是否有效移動.

  3. varcurItem=getCurItem(objid);

  4. varcurIndex=curItem.index;

  5. for(vari=0;i<indexList.length;i++){

  6. varmiIndex=parseInt(indexList[i]);

  7. if((curIndex+1)==miIndex&&(curIndex+1)%colSize==0){

  8. alertInfo("最後一列不能調大!");

  9. returnfalse;

  10. }

  11. if(miIndex>=(rowSize*colSize)){

  12. alertInfo("不能超過三行!");

  13. returnfalse;

  14. }

  15. varcellnum=getItemCellNum(miIndex);

  16. if(cellnum!=1){

  17. alertInfo("被移動單元格目前只支持1個單元格!");

  18. returnfalse;

  19. }

  20. }

  21. //判斷是否超過所規定顯示區域

  22. varnullnum=0;

  23. varnullIndexArray=newArray();

  24. for(vari=0;i<itemArray.length;i++){

  25. vartmpItem=itemArray[i];

  26. varobjvalue=tmpItem.value;

  27. if(itemvalueforspace==objvalue||itemvaluefornull==objvalue){

  28. varisexitarea=false;

  29. for(varj=0;j<indexList.length;j++){

  30. varmiIndex=parseInt(indexList[j]);

  31. if(i==miIndex){

  32. isexitarea=true;

  33. break;

  34. }

  35. }

  36. nullnum=nullnum+1;

  37. if(!isexitarea){

  38. nullIndexArray.push(i);

  39. }

  40. }

  41. }

  42. if((itemArray.length-nullnum+indexList.length)>(rowSize*colSize)){

  43. alertInfo("調大單元格已超出所顯示區域!");

  44. returnfalse;

  45. }

  46. //調整移動單元格的隊列.

  47. for(vari=0;i<indexList.length;i++){

  48. varmiIndex=parseInt(indexList[i]);

  49. varmoveItem=itemArray[miIndex];

  50. if(moveItem==undefined){//不存在.

  51. for(varj=itemArray.length;j<=miIndex;j++){

  52. addNullItem("");

  53. }

  54. moveItem=itemArray[miIndex];

  55. }

  56. //undefined

  57. varmoveValue=moveItem.value;

  58. moveItem.value=curItem.value;

  59. itemArray[miIndex]=moveItem;

  60. if(moveValue!=itemvalueforspace){//覆蓋單元格中已有對象.

  61. //覆蓋單元格移到最後

  62. varmoveIndex=itemArray.length;

  63. varcol=moveIndex%colSize;

  64. varrow=(moveIndex-col)/colSize;

  65. varmoveLeft=splitspace+col*(initwidth+splitspace);

  66. varmoveTop=splitspace+row*(initheight+splitspace);

  67. varmoveCacheItem=newObject();

  68. moveCacheItem.index=moveIndex;

  69. moveCacheItem.id=itemPrefix+(moveIndex+1);

  70. moveCacheItem.x=moveLeft;

  71. moveCacheItem.y=moveTop;

  72. moveCacheItem.value=moveValue;

  73. itemArray.push(moveCacheItem);

  74. varmoveObj=document.getElementById(moveValue);

  75. moveObj.style.top=moveTop+"px";

  76. moveObj.style.left=moveLeft+"px";

  77. }else{//null單元格,需要刪除null單元格.

  78. //del(moveItem.id);

  79. }

  80. }

  81. //檢查空缺單元格,如果存在,就填充null對象.

  82. for(vari=itemArray.length-1;i>=(rowSize*colSize);i--){

  83. varmovitem=itemArray[i];

  84. varnulitemIndex=nullIndexArray[nullIndexArray.length-1];

  85. varnulitem=itemArray[nulitemIndex];

  86. varmoveObj=document.getElementById(movitem.value);

  87. //console.log("id="+movitem.id+"//value="+movitem.value+"//top="+nulitem.y+"//left="+nulitem.x);

  88. moveObj.style.top=parseInt(nulitem.y)+"px";

  89. moveObj.style.left=parseInt(nulitem.x)+"px";

  90. //console.log("nullid="+nulitem.id+"//moveid="+movitem.id+"//value="+movitem.value+"//top="+nulitem.y+"//left="+nulitem.x);

  91. getElement(sortablecurid).removeChild(getElement(nulitem.id));

  92. nulitem.value=movitem.value;

  93. itemArray[nulitemIndex]=nulitem;

  94. itemArray.pop();

  95. nullIndexArray.pop();

  96. }

  97. printItemArray();

  98. returntrue;

  99. }

function moveItem(objid,indexList){
	//判斷是否有效移動.
	var curItem=getCurItem(objid);
	var curIndex=curItem.index;	
	for(var i=0;i<indexList.length;i++){
		var miIndex=parseInt(indexList[i]);
		if((curIndex+1)==miIndex&&(curIndex+1)%colSize==0){
			alertInfo("最後一列不能調大!");
			return false;
		}
		if(miIndex>=(rowSize*colSize)){
			alertInfo("不能超過三行!");
			return false;
		}
		var cellnum=getItemCellNum(miIndex);
		if(cellnum!=1){
			alertInfo("被移動單元格目前只支持1個單元格!");
			return false;
		}
	}
	//判斷是否超過所規定顯示區域
	var nullnum=0;
	var nullIndexArray=new Array();
	for(var i=0;i<itemArray.length;i++){
		var tmpItem=itemArray[i];	
		var objvalue=tmpItem.value;
		if(itemvalueforspace==objvalue||itemvaluefornull==objvalue){
			var isexitarea=false;
			for(var j=0;j<indexList.length;j++){
				var miIndex=parseInt(indexList[j]);
				if(i==miIndex){
					isexitarea=true;
					break;
				}
			}
			nullnum=nullnum+1;
			if(!isexitarea){				
				nullIndexArray.push(i);
			}
		}	
	}
	if((itemArray.length-nullnum+indexList.length)>(rowSize*colSize)){
		alertInfo("調大單元格已超出所顯示區域!");
		return false;
	}
	//調整移動單元格的隊列.
	for(var i=0;i<indexList.length;i++){
		var miIndex=parseInt(indexList[i]);
		var moveItem=itemArray[miIndex];
		if(moveItem==undefined){//不存在.
			for(var j=itemArray.length;j<=miIndex;j++){
				addNullItem("");
			}
			moveItem=itemArray[miIndex];
		}
		//undefined
		var moveValue=moveItem.value;
		moveItem.value=curItem.value;		
		itemArray[miIndex]=moveItem;
		if(moveValue!=itemvalueforspace){//覆蓋單元格中已有對象.
			//覆蓋單元格移到最後	
			var moveIndex=itemArray.length;		
			var col=moveIndex%colSize;
			var row=(moveIndex-col)/colSize;
			var moveLeft=splitspace+col*(initwidth+splitspace);
			var moveTop=splitspace+row*(initheight+splitspace);
			var moveCacheItem=new Object();
			moveCacheItem.index=moveIndex;
			moveCacheItem.id=itemPrefix+(moveIndex+1);
			moveCacheItem.x=moveLeft;
			moveCacheItem.y=moveTop;
			moveCacheItem.value=moveValue;
			itemArray.push(moveCacheItem);
			var moveObj=document.getElementById(moveValue);
			moveObj.style.top=moveTop+"px";
			moveObj.style.left=moveLeft+"px";	
		}else{//null單元格,需要刪除null單元格.
			//del(moveItem.id);
		}
		
	}

	//檢查空缺單元格,如果存在,就填充null對象.
	for(var i=itemArray.length-1;i>=(rowSize*colSize);i--){
		var movitem=itemArray[i];
		var nulitemIndex=nullIndexArray[nullIndexArray.length-1];
		var nulitem=itemArray[nulitemIndex];
		var moveObj=document.getElementById(movitem.value);
		//console.log("id="+movitem.id+"//value="+movitem.value+"//top="+nulitem.y+"//left="+nulitem.x);
		moveObj.style.top=parseInt(nulitem.y)+"px";
		moveObj.style.left=parseInt(nulitem.x)+"px";
		//console.log("nullid="+nulitem.id+"//moveid="+movitem.id+"//value="+movitem.value+"//top="+nulitem.y+"//left="+nulitem.x);
		getElement(sortablecurid).removeChild(getElement(nulitem.id));
		nulitem.value=movitem.value;
		itemArray[nulitemIndex]=nulitem;
		itemArray.pop();
		nullIndexArray.pop();
	}	

	printItemArray();
	return true;
}


待續……

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章