我是jQuery的新手,并且已经通过Codecademy学习它.我正在网站上创建一个'codebit'(网站),我试图在按下向上,向下,向左和向右键时使图像精灵做出反应(移动).
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Sprite</title>
<link rel='stylesheet' type='text/css' href='style.css'/>
<script type='text/javascript' src='script.js'></script>
</head>
<body>
<img src="[img]"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
CSS:
img {
position: relative;
left: 0;
top: 0;
}
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
$(document).ready(function() {
$(document).keydown(function(key) {
switch(parseInt(key.which,10)) {
//LEFT
case 37:
$('img').animate({left: "-=10px"}, 500);
break;
//RIGHT
case 39:
$('img').animate({left: "+=10px"}, 500);
break;
//UP
case 38:
$('img').animate({top: "-=10px"}, 500);
break;
//DOWN
case 40:
$('img').animate({top: "+=10px"}, 500);
break;
}
});
});
Run Code Online (Sandbox Code Playgroud)
我已经检查了一些网站上的语法错误,似乎找不到任何明显的错误.帮助将非常感激.
我正在开发一个模拟控制台窗口的项目。当用户按下 Enter<input>键时,屏幕上会添加一个新的文本字段并聚焦。唯一的问题是之前<input>字段的值在此过程中丢失了。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css'/>
<script src='script.js'></script>
</head>
<body>
<input type="text" class="console_window"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
var input;
function init() {
function keyup(e) {
switch(e.which) {
case 13:
document.body.innerHTML +=
"<input type='text' class='console_window'/>"
input = document.getElementsByClassName("console_window")[document.getElementsByClassName("console_window").length-1];
input.focus();
break;
}
}
document.addEventListener("keyup", keyup, false);
}
document.addEventListener("DOMContentLoaded", init, false);
Run Code Online (Sandbox Code Playgroud)
和小提琴。
另外,我不喜欢使用插件。
谢谢!