javascript下用鍵盤控制層的移動的代碼

來源:互聯網
上載者:User

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>鍵盤控制層的移動</title>
<style type="text/css">
<!--
#Div {
position:absolute;
width:238px;
height:135px;
left:expression((body.clientWidth - this.offsetWidth)/2);
top:expression((body.clientHeight - this.offsetHeight)/2);
z-index:1;
text-align:center;
background:#AFDBFF;
border:#006699 1px solid;
}
#Txt{
font-size:9pt;
position:absolute;
width:200px;
top:expression((Div.offsetHeight - this.offsetHeight)/2);
left:expression((Div.offsetWidth - this.offsetWidth)/2);
}
#Txt p{
font-size:9pt;
margin:8px;
}
#Txt font{
color:#FF0000;
font-size:9pt;
}
#Layer1 {
border:#006699 1px solid;
padding:10px;
font-size:9pt;
color:#336699;
position:absolute;
top:expression((body.clientHeight - this.offsetHeight)/2);
left:expression((body.clientWidth - this.offsetWidth)/2 + Div.offsetWidth);
}
#Layer1 Input {
font-size:9pt;
color:#336699;
}
#Int {
text-align:right;
}
-->
</style>
</head>
<body>
<div id="Div"><span id="Txt"><p>請分別按下 <font>↑</font>、<font>↓</font>、<font>←</font>、<font>→</font>,</p><p>
試試看有什麼效果? </p></span></div>
<span id="Layer1">修飾鍵設定:<br>
向上移動:
<input name="Up" type="text" value="↑" size="6"><br>
向下移動:
<input name="Down" type="text" value="↓" size="6"><br>
向左移動:
<input name="Left" type="text" value="←" size="6"><br>
向右移動:
<input name="Right" type="text" value="→" size="6"><br>
每次移動 <input name="Int" type="text" id="Int" value="5" size="4" maxlength="3">
px;</span>
</body>
</html>
<script language="javascript">
<!--
//alert(txt.style.top);
var up,down,left,right;
up = 38;
down = 40;
left = 37;
right = 39;
function document.onkeydown()
{
// alert(event.keyCode);
//左:37 上:38 右:39 下:40
var int;
int = parseInt(document.getElementById("Int").value);
if(int == "NaN")
int = 5;
var str = "",press,evet;
var div = document.getElementById("Div");
var txt = document.getElementById("Txt");
if(event.srcElement.tagName == "INPUT")
{
if(event.srcElement == document.getElementById("Int"))
{
if(event.keyCode == 13)
document.body.focus();
if((event.keyCode < 96 || event.keyCode > 105) && event.keyCode != 8 && event.keyCode != 46 && !(event.keyCode >= 37 && event.keyCode <= 40) )
event.returnValue = false;
return;
}
else
{
switch(event.keyCode)
{
case 37:{
event.srcElement.value = "←";
break;
}
case 38:{
event.srcElement.value = "↑";
break;
}
case 39:{
event.srcElement.value = "→";
break;
}
case 40:{
event.srcElement.value = "↓";
break;
}
}
switch(event.srcElement)
{
case document.getElementById("Up"):{
up = event.keyCode;
break;
}
case document.getElementById("Down"):{
down = event.keyCode;
break;
}
case document.getElementById("Left"):{
left = event.keyCode;
break;
}
case document.getElementById("Right"):{
right = event.keyCode;
break;
}
}
}
}
else
{
switch(event.keyCode)
{
case left:{
press = "<font> ← </font>";
evet = "<font>向 <b>左</b> 移動</font>" + " " + int + " px 。";
div.style.left = (parseInt(div.currentStyle.left) - int) + "px";
if(parseInt(div.style.left) <= 0)
{
evet = "已經到了 <font>最左邊</font> ,無法再 <font>向左</font> 移動。";
div.style.left = "0px";
break ;
}
break;
}
case up:{
press = "<font> ↑ </font>";
evet = "<font>向 <b>上</b> 移動</font>" + " " + int + " px 。";
div.style.top = (parseInt(div.currentStyle.top) - int) + "px";
if(parseInt(div.style.top) <= 0)
{
evet = "已經到了 <font>最上邊</font> ,無法再 <font>向上</font> 移動。";
div.style.top = "0px";
break ;
}
break;
}
case right:{
press = "<font> → </font>";
evet = "<font>向 <b>右</b> 移動</font>" + " " + int + " px 。";
div.style.left = (parseInt(div.currentStyle.left) + int) + "px";
if(parseInt(div.style.left) >= (parseInt(document.body.clientWidth) - parseInt(div.offsetWidth)))
{
div.style.left = parseInt(document.body.clientWidth) - parseInt(div.offsetWidth);
evet = "已經到了 <font>最右邊</font> ,無法再 <font>向右</font> 移動。";
break ;
}
break;
}
case down:{
press = "<font> ↓ </font>";
evet = "<font>向 <b>下</b> 移動</font>" + " " + int + " px 。";
div.style.top = (parseInt(div.currentStyle.top) + int) + "px";
if(parseInt(div.style.top) >= (parseInt(document.body.clientHeight) - parseInt(div.offsetHeight)))
{
div.style.top = parseInt(document.body.clientHeight) - parseInt(div.offsetHeight);
evet = "已經到了 <font>最下邊</font> ,無法再 <font>向下</font> 移動。";
break ;
}
break;
}
default:
{
return;
}
}
str = "<p>您按下了" + press + "鍵,</p>";
str += "<p>該圖層" + evet;
txt.innerHTML = str;
}
}
//-->
</script> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"><br /><style type="text/css"> <!-- #Div { position:absolute; width:238px; height:135px; left:expression((body.clientWidth - this.offsetWidth)/2); top:expression((body.clientHeight - this.offsetHeight)/2); z-index:1; text-align:center; background:#AFDBFF; border:#006699 1px solid; } #Txt{ font-size:9pt; position:absolute; width:200px; top:expression((Div.offsetHeight - this.offsetHeight)/2); left:expression((Div.offsetWidth - this.offsetWidth)/2); } #Txt p{ font-size:9pt; margin:8px; } #Txt font{ color:#FF0000; font-size:9pt; } #Layer1 { border:#006699 1px solid; padding:10px; font-size:9pt; color:#336699; position:absolute; top:expression((body.clientHeight - this.offsetHeight)/2); left:expression((body.clientWidth - this.offsetWidth)/2 + Div.offsetWidth); } #Layer1 Input { font-size:9pt; color:#336699; } #Int { text-align:right; } --> </style><p>請分別按下 ↑、↓、←、→,</p><p>試試看有什麼效果? </p><p><p>修飾鍵設定:</p><p>向上移動:<br /><input name="Up" type="text" value="↑" size="6"></p><p>向下移動:<br /><input name="Down" type="text" value="↓" size="6"></p><p>向左移動:<br /><input name="Left" type="text" value="←" size="6"></p><p>向右移動:<br /><input name="Right" type="text" value="→" size="6"></p><p>每次移動 <input name="Int" type="text" id="Int" value="5" size="4" maxlength="3"><br />px;<br />
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.