首頁 >web前端 >js教程 >js排序動畫模擬-插入排序_javascript技巧

js排序動畫模擬-插入排序_javascript技巧

WBOY
WBOY原創
2016-05-16 17:51:511456瀏覽

[Ctrl A 全選 註:如需引入外部Js需刷新才能執行
]<script><!-- window.onload = function(){ var mop = document.getElementById("mop"),frag = document.createDocumentFragment(); (function(){ var li , a; for(var i = 0; i < 25; i++){ li = document.createElement("li");//YI.newLi({className:"fac",id:"f" + i})[0]; li.className = "fac"; li.id = "f" + i; a = document.createElement("a"); li.appendChild(a); frag.appendChild( li ); } mop.appendChild(frag); })(); var facs = document.getElementsByTagName("li"), set = [], steps = []; jOne.each(facs,function(i){ var hsb="rgb(" +rand(0, 200)+","+ rand(50, 250) + ", " + rand(50, 200) + ")",// h = rand(100, 500); set.push(h); jOne.setStyle(this, {height:h + "px",left:i * 25+300}); jOne.setStyle(this.children[0], {background:hsb,width:"25px",height:h + "px"}); }); var queue = []; var bub = bubble(); bub.sort(set,queue); var dq = createDq(); dq.setDq(queue); dq.run(function(){ var reg = /(\:)|(\^)|(\-)/,arg = arguments[0],ids, elem1, elem2, ret = arg.match(reg); if(ret[1]){ ids = arg.split(/\:/); elem2 = document.getElementById("f" + ids[1]); dq.setCl( elem2.children[0] ); return; } if(ret[2]){ ids = arg.split(/\^/); elem1 = document.getElementById("f" + ids[0] ); if(ids[0] == ids[1]) return; var cl = dq.getCl(); if(elem1.children.length ){ elem1.removeChild(elem1.children[0]); elem1.appendChild(cl); }else{ elem1.appendChild(cl); } return; } if(ret[3]){ ids = arguments[0].split(/\-/); elem1 = document.getElementById("f" + ids[0] ); elem2 = document.getElementById("f" + ids[1] ); if(ids[0] == ids[1]) return; if(elem1.children.length){ elem1.removeChild(elem1.children[0]); elem1.appendChild(elem2.children[0]); }else{ elem1.appendChild(elem2.children[0]); } } }); }; function createDq(){ var dq = [], size = 0, cl; return { setDq:function(queue){ dq = queue; size = queue.length; }, queue:function(fn){ size ++; dq.push(fn); }, dqueue:function(){ size --; return dq.shift(); }, run:function(fn){ var me = this, timer; timer = setInterval(function(){ if(size <= 1){ clearInterval(timer); } fn.call(null,me.dqueue()); },100); }, setCl:function(elem){ cl = elem; }, getCl:function(){ return cl; } } } function bubble(){ var obs = []; return { addOb:function(ob){ obs.push(ob); }, sort:function(arr,q){ var len = arr.length, i = 1, j, t, cl; for(; i < len; i++){ t = arr[i]; q.push("clone:" + i); j = i ; while(j >0 && (t < (arr[j - 1]))){ q.push(j + "-" + (j - 1)); arr[j] = arr[j-1]; j--; } q.push(j + "^" + i); arr[j] = t; } }//sort }//return } var jOne = { each:function(obj,callback){ var i = 0,len = obj.length; for(var value = obj[0]; i < len && callback.call(value,i,value) !==false ; value = obj[ ++i ] ){}; }, setStyle:function(elem, opt){ var o, sty = elem.style; for(var o in opt){ sty[o] = opt[o] } } } function rand(a, b){ var ret = Math.random() * (b - a) + a; return ret >> 0; } //--></script>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn