本文實例講述了jQuery實現Div拖曳 鍵盤控制綜合效果的方法。分享給大家供大家參考。具體實作方法如下: 複製程式碼 程式碼如下: jQuery Div拖动+键盘控制综合效果 预览时看不到效果,刷新一下即可 <br> var msgObj = document.getElementById('text');<br> var posx = 0;<br> var posy = 0;<br> var moveable = false;<br> var MouseDownEvent = document.onmousedown;<br> var MouseMoveEvent = document.onmousemove;<br> var MouseUpEvent = document.onmouseup;<br> msgObj.onmousedown = function(evt) {<br> var evt = evt||window.event;<br> moveable = true; <br> posy = evt.clientY-parseInt(msgObj.style.top);<br> posx = evt.clientX-parseInt(msgObj.style.left);<br> document.onmousemove = function(evt) {<br> if (moveable) {<br> var evt = evt||window.event;<br> msgObj.style.left = evt.clientX - posx + "px";<br> msgObj.style.top = evt.clientY - posy + "px";<br> }<br> change();<br> };<br> document.onmouseup = function () { <br> if (moveable) { <br> document.onmousemove = MouseMoveEvent;<br> document.onmouseup = MouseUpEvent;<br> moveable = false; <br> posx = 0;<br> posy = 0;<br> } <br> };<br> }<br> var v=1;<br> var a=0.9;<br> var h=document.documentElement.clientHeight;<br> function scroll(){<br> var timer=setInterval(function(){<br> v +=a;<br> var top=(parseInt(msgObj.style.top)||0);<br> if(top+v>h-70){<br> if(v<2)clearInterval(timer);<br /> msgObj.style.top=h-70+"px";<br /> v=-v*0.5; <br /> }else{<br /> msgObj.style.top=top+v+"px";<br /> }<br /> change();<br /> },10);<br /> };<br /> $(document).keyup(function(e){<br /> var e = e || window.event ;<br /> if(e.which == 13){<br /> scroll(); <br /> }<br /> })<br /> var x = 4 ;<br /> var y = 4 ;<br /> function newpro(){<br /> var Height = document.documentElement.clientHeight ;<br /> var Width = document.documentElement.clientWidth ;<br /> var newobj = document.createElement('div');<br /> newobj.setAttribute('id','bigbox');<br /> newobj.style.position = 'absolute';<br /> newobj.style.left = 20 +"px";<br /> newobj.style.top = 80 +"px";<br /> newobj.style.height = Height/x +"px" ;<br /> newobj.style.width = Width/y +"px" ;<br /> newobj.style.borderWidth = 1+"px";<br /> newobj.style.borderColor = "#ff6500";<br /> newobj.style.borderStyle = 'solid' ;<br /> newobj.innerHTML='<div id="minbox"></div>';<br> document.body.appendChild(newobj);<br> change();<br> }<br> function change(){<br> var minobj = document.getElementById('minbox');<br> minobj.style.position = 'absolute';<br> minobj.style.left = parseInt(msgObj.style.left)/x "px";<br> minobj.style.top = parseInt(msgObj.style.top)/y "px";<br> minobj.style.height = 20 "px" ;<br> minobj.style.width = 30 "px" ;<br> minobj.style.background = "#F06";<br> }<br> window.onload = function(){<br> newpro();<br> }<br> <br> </div>