Salin kod Kod adalah seperti berikut: http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> http://www.w3.org/1999/xhtml"> Table <br> body{margin:0px; padding:0px; font-size:12px}<br> .div{height:160px;width:160px;position:absolute; text-align:center; }<br> .demo1{ border:1px solid #96C2F1;background-color:#EFF7FF;left:150px;top:20px}<br> .demo2{ border:1px solid #9BDF70;background-color:#F0FBEB;left:450px;top:20px}<br> .demo3{ border:1px solid #BBE1F1;background-color:#EEFAFF;left:750px;top:20px}<br> .demo4{ border:1px solid #96C2F1;background-color:#EEFAFF;left:150px;top:220px}<br> .demo5{ border:1px solid #FFCC00;background-color:#FFFFF7;left:450px;top:220px}<br> .demo6{ border:1px solid #E3E197;background-color:#FFFFDD;left:750px;top:220px}<br> .demo7{ border:1px solid #ADCD3C;background-color:#F2FDDB;left:150px;top:420px}<br> .demo8{ border:1px solid #F8B3D0;background-color:#FFF5FA;left:450px;top:420px}<br> .demo9{ border:1px solid #D3D3D3;background-color:#F7F7F7;left:750px;top:420px}<br> .focus{background-color: #990000;}<br> demo1 demo2 demo3 demo4 demo5 demo6 demo7 demo8 demo9 <br> (fungsi(tetingkap, tidak ditentukan){<br> window.Sys = fungsi (ua){<br> var b = {<br> iaitu: /msie/.test(ua) && !/opera/.test(ua),<br> opera: /opera/.test(ua),<br> safari: /webkit/.test(ua) && !/chrome/.test(ua),<br> firefox: /firefox/.test(ua),<br> chrome: /chrome/.test(ua)<br> },vMark = "";<br> untuk (var i dalam b) {<br> jika (b[i]) { vMark = "safari" == i ? "versi" : i; pecah; }<br> }<br> b.version = vMark && RegExp("(?:" vMark ")[\/: ]([\d.] )").test(ua) ? RegExp.$1 : "0";<br> b.ie6 = b.ie && parseInt(b.version, 10) == 6;<br> b.ie7 = b.ie && parseInt(b.version, 10) == 7;<br> b.ie8 = b.ie && parseInt(b.version, 10) == 8; <br> kembali b;<br> }(window.navigator.userAgent.toLowerCase()); <p>window.Sys.ie6&&document.execCommand("BackgroundImageCache", false, true);</p> <p>tetingkap.$ = function(Id){<br> pulangkan document.getElementById(Id);<br> };<br> window.addListener = fungsi(elemen,e,fn){<br> !element.events&&(element.events = {});<br> element.events[e]&&(element.events[e][addListener.guid ]=fn)||(element.events[e] = {'0':fn});<br> element.addEventListener?element.addEventListener(e,fn,false):element.attachEvent("on" e,fn);<br> };<br> window.addListener.guid = 1;<br> window.removeListener = fungsi(elemen,e,fn){<br> pengendali var = element.events[e],type;<br> jika(fn){<br> untuk(taipkan pengendali)<br> if(pengendali[jenis]===fn){<br> element.removeEventListener?element.removeEventListener(e,fn,false):element.detachEvent("on" e,fn);<br> padamkan pengendali[jenis];<br> }<br> }lain{<br> untuk(taipkan pengendali){<br> element.removeEventListener?element.removeEventListener(e,pengendali[jenis],false):element.detachEvent("on" e,pengendali[jenis]);<br> padamkan pengendali[jenis];<br> }<br> } <br> };<br> window.setStyle = fungsi(e,o){<br> if(typeof o=="string")<br> e.style.cssText=o;<br> lain <br> untuk(var i dalam o)<br> e.gaya[i] = o[i];<br> };</p> <p>var slice = Array.prototype.slice; <br> window.Bind = fungsi(objek, keseronokan) {<br> var args = slice.call(arguments).slice(2);<br> kembalikan fungsi() {<br> kembalikan fun.apply(objek, args);<br> };<br> };<br> window.BindAsEventListener = fungsi(objek, keseronokan,args) {<br> var args = slice.call(arguments).slice(2);<br> kembalikan fungsi(acara) {<br> kembalikan fun.apply(objek, [event || window.event].concat(args));<br> }<br> };<br> //salin daripada jQ<br> window.extend = function(){<br> var target = arguments[0] || {}, i = 1, length = arguments.length, deep = true, pilihan;<br> jika ( jenis sasaran === "boolean") {<br> dalam = sasaran;<br> sasaran = hujah[1] || {};<br> i = 2;<br> }<br> jika ( jenis sasaran !== "objek" && Object.prototype.toString.call(target)!="[object Function]")<br> sasaran = {};<br> untuk(;i<panjang;i ){<br /> jika ( (pilihan = hujah[ i ]) != batal )<br /> for(nama var dalam pilihan){<br /> var src = sasaran[ nama ], salinan = pilihan[ nama ];<br /> jika ( sasaran === salin )<br /> teruskan;<br /> jika ( dalam && salin && jenis salinan === "objek" && !copy.nodeType ){<br /> target[ name ] = arguments.callee( deep, src || ( copy.length != null ? [ ] : { } ), copy );<br /> } <br /> else if(copy !== undefined)<br /> sasaran[ nama ] = salinan; <br /> }<br /> }<br /> sasaran pulangan; <br /> };<br /> //salin daripada jQ<br /> window.each = fungsi ( objek, panggil balik, args ) { <br /> nama var, i = 0, panjang = objek.panjang; <br /> jika ( args ) {<br /> args = Array.prototype.slice.call(arguments).slice(2);<br /> jika ( panjang === tidak ditentukan ) { <br /> untuk ( nama dalam objek ) <br /> if ( callback.apply( object[ name ],[name,object[ name ]].concat(args) ) === false ) <br /> pecah; <br /> } lain <br /> untuk ( ; i < panjang; i ) <br /> if ( callback.apply( object[ i ],[i,object[ i ]].concat(args)) === false ) //<br /> pecah; <br /> } lain { <br /> jika ( panjang === tidak ditentukan ) { <br /> untuk ( nama dalam objek ) <br /> if ( callback.call( object[ name ], name, object[ name ] ) === false ) <br /> pecah; <br /> } lain <br /> untuk ( nilai var = objek[0]; <br /> i < panjang && panggil balik.panggilan( nilai, i, nilai ) !== palsu; nilai = objek[ i] ){} <br /> } <br /> objek kembali; <br /> }; <br /> window.currentStyle = fungsi(elemen){<br /> kembalikan elemen.currentStyle || document.defaultView.getComputedStyle(elemen, null);<br /> };<br /> window.objPos = fungsi(elemen){<br /> var kiri = 0, atas = 0, kanan = 0, bawah = 0, doc = elem ? elem.ownerDocument : document;<br /> if ( !elem.getBoundingClientRect || window.Sys.ie8 ) {<br /> var n = elem;<br /> manakala (n) { kiri = n.offsetLeft, atas = n.offsetTop; n = n.offsetParent; };<br /> kanan = kiri elem.offsetWidth; bawah = elem atas.offsetHeight;<br /> } lain {<br /> var rect = elem.getBoundingClientRect();<br /> kiri = kanan = doc.documentElement.scrollLeft || doc.body.scrollKiri;<br /> atas = bawah = doc.documentElement.scrollLeft || doc.body.scrollKiri;<br /> kiri = rect.left; kanan = rect.right;<br /> atas = rect.top; bawah = rect.bottom;<br /> }<br /> kembali { "kiri": kiri, "atas": atas, "kanan": kanan, "bawah": bawah }; <br /> };<br /> window.hasClass = fungsi(elemen,Nama kelas){<br /> return element.className.match(new RegExp('(<a href="file://\s|^)' className '(\s|$)'">\s|^)' className '(\s|$)'</a>));<br /> };<br /> window.addClass = function(elemen, className){<br /> !window.hasClass(elemen, className)&&(element.className = " " className);<br /> };<br /> window.removeClass = fungsi(elemen,Nama kelas){<br /> window.hasClass(elemen, className)&&(element.className = element.className.replace(new RegExp('(<a href="file://\s|^)' className '(\s|$)'),'">\s|^)' className '(\s|$)'),'</a> ' ));<br /> }<br /> })(tetingkap);</p><p>var Seret = {<br /> elem : batal,<br /> zindex : 0,<br /> pilihan : {},<br /> init : function(){ <br /> setiap (hujah, fungsi (i, elem, oIni){<br /> addListener(elem,'mousedown',BindAsEventListener(oThis,oThis.start,elem));<br /> },ini);<br /> },<br /> mula : fungsi(e,elem){<br /> var elem=this.elem = elem;<br /> elem.style.zIndex= this.zindex;<br /> this.x = e.clientX - elem.offsetLeft ;<br /> this.y = e.clientY - elem.offsetTop;<br /> this.marginLeft = parseInt(currentStyle(elem).marginLeft)||0;<br /> this.marginTop = parseInt(currentStyle(elem).marginTop)||0;<br /> Sys.ie?elem.setCapture():e.preventDefault();<br /> addListener(dokumen,"mousemove",BindAsEventListener(this,this.move));<br /> addListener(dokumen,"mouseup",Bind(this,this.up)); <br /> this.options.callbackmove&&this.options.callbackmove(this.elem);<br /> },<br /> gerakkan : fungsi(e){<br /> window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();<br /> var iLeft = e.clientX - this.x,iTop = e.clientY - this.y;obj = this.elem;<br /> obj.style.left = iLeft - this.marginLeft "px";<br /> obj.style.top = iTop - this.marginTop "px";<br /> this.options.callbackmove&&this.options.callbackmove(this.elem);<br /> },<br /> atas : function(){<br /> removeListener(dokumen,'mousemove');<br /> removeListener(dokumen, 'mouseup');<br /> Sys.ie&&this.elem.releaseCapture();<br /> this.options.callbackup&&this.options.callbackup(this.elem);<br /> }<br /> };</p><p>bertindih var = {<br /> asrama :{}, //所有需要计算重合的元素<br /> senarai tindih :{}, //已经重合的元素<br /> init: fungsi(elem){<br /> setiap(elem,fungsi(i,elem,oIni){<br /> elem.setAttribute('tindih',i);<br /> var ret = objPos(elem),l=ret.left,t=ret.top,b=ret.bottom,r=ret.right;<br /> oThis.hostel[i]={elem:elem,leftTopDot:{x:l,y:t},leftBottomDot:{x:l,y:b},rightTopDot:{x:r,y:t},rightBottomDot: {x:r,y:b}};<br /> },ini);<br /> },<br /> setElem:function(elem){<br /> jika(!elem)kembali;<br /> var ret = objPos(elem),l=ret.left,t=ret.top,b=ret.bottom,r=ret.right;<br /> this.hostel[elem.getAttribute('overlap')] ={elem:elem,leftTopDot:{x:l,y:t},leftBottomDot:{x:l,y:b},rightTopDot:{x:r, y:t},rightBottomDot:{x:r,y:b}};<br /> },<br /> //判断是否重合 <br /> isOverlap : function(my){<br /> var obj= {}, my = this.hostel[my.getAttribute('overlap')];<br /> <br /> setiap (ini.asrama,fungsi(kunci,konfigurasi,oIni){<br /> // 是元素本身 则返回<br /> if(config.elem === my.elem)kembali ;<br /> <br /> //判断2个div是否重合 如果不重合 则返回<br /> if(my.leftBottomDot.y<=config.leftTopDot.y||my.leftTopDot.y>=config.leftBottomDot.y||my.rightTopDot.x<=config.leftTopDot.x||my.leftTopDot.x&got; =config.rightTopDot.x)<br /> kembali;<br /> obj[config.elem.getAttribute('overlap')] = [config.elem,oThis.howOverlap(my,config)];<br /> },ini);<br /> pulangkan obj;<br /> },<br /> //判断重合面积<br /> howOverlap : function(my, other){<br /> var l=other.leftBottomDot.x,r=other.rightTopDot.x,t=other.leftTopDot.y,b=other.leftBottomDot.y,arr=[],<br /> lt = this.include(my.leftTopDot,l,r,t,b,'leftTopDot-rightBottomDot'),<br /> lb = this.include(my.leftBottomDot,l,r,t,b,'leftBottomDot-rightTopDot'),<br /> rt = this.include(my.rightTopDot,l,r,t,b,'rightTopDot-leftBottomDot'),<br /> rb = this.include(my.rightBottomDot,l,r,t,b,'rightBottomDot-leftTopDot');<br /> lt&&arr.push(lt)||lb&&arr.push(lb)||rt&&arr.push(rt)||rb&&arr.push(rb);<br /> <br /> if(!arr[0]) return 0;<br /> <br /> if(arr.length====1||arr.length====2){<br /> var key = arr[0].split('-'),x1=my[key[0]].x,y1=my[key[0]].y,x2=other[key[1]].x ,y2=lain[kunci[1]].y;<br /> kembalikan Math.abs((x1-x2)*(y1-y2)); <br /> }; <br /> //完全重合<br /> if(arr.length====4){<br /> kembali 162*162; <br /> }; <br /> },<br /> //看点是不是在另一个div中<br /> sertakan : fungsi(titik,l,r,t,b,kunci){<br /> kembali (dot.x>=l&&dot.x<=r&&dot.y>=t&&dot.y<=b)?key:false;<br /> }<br /> };<br /> window.onload = function(){<br /> lanjutkan(Drag.options,{callbackmove:move,callbackup:up});<br /> <br /> fungsi atas(elem){<br /> untuk(var n dalam tindih. Senarai tindih)<br /> removeClass(overlap.overlapList[n][0],'focus')<br /> overlap.overlapList = {};<br /> Drag.elem.innerHTML =Drag.elem.id;<br /> };<br /> <br /> Pergerakan fungsi(elemen){<br /> overlap.setElem(elem);<br /> //p digunakan untuk menentukan sama ada obj yang dikembalikan kosong<br /> var obj = overlap.isOverlap(elem),name,p = function(o){<br /> untuk (nama dalam o)<br /> pulangan palsu;<br /> kembali benar;<br /> }(obj); <br /> //Jika ia adalah objek kosong, kembali dan jangan lakukan traversal berikut<br /> jika(p){<br /> atas();<br /> kembali;<br /> };<br /> <br /> var str =''; overlap.overlapList = obj;<br /> setiap satu(overlap.hostel,function(key,config){<br /> Jika(obj[kunci]){<br /> addClass(obj[key][0],'focus');<br /> str = str 'Kawasan yang bertindih ' obj[key][0].id ' ialah:' obj[key][1] '</br>';<br> } lain{<br> removeClass(config.elem,'focus');<br> }<br> });<br> Drag.elem.innerHTML = str;<br> };<br> Drag.init($('demo1'),$('demo2'),$('demo3'),$('demo4'),$('demo5'),$('demo6'),$('demo7 '),$('demo8'),$('demo9'));<br> overlap.init([$('demo1'),$('demo2'),$('demo3'),$('demo4'),$('demo5'),$('demo6'),$(' demo7'),$('demo8'),$('demo9')]);<br> };<br> </skrip><br> </body><br> </html><br> <br><br> </p> </div>Kodnya seperti di atas, tetapi rasanya tidak cekap. Adakah anda mempunyai sebarang rancangan pengoptimuman. Sila maklumkan kepada saya. Terima kasih banyak