<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>可以移动的箱子</title>
#my {
width: 0;
height: 0;
border-top: solid 60px #ccc;
border-right: solid 60px black;
border-bottom: solid 60px rgb(0, 122, 204);
border-left: solid 60px orange;
position: absolute;
}
</head>
<body>
<div id="my">
</div
<script>
'use strict';
var l = 300,
f = 300;
window.onkeydown = function(event) {
var my = document.getElementById('my')
if (event.key == 'a') {
l = l - 10
}
if (event.key == 'd') {
l = l + 10
}
if (event.key == 'w') {
f = f - 10
}
if (event.key == 's') {
f = f + 10
}
my.style.left = l + 'px';
my.style.top = f + 'px';
}
</script>
</body>
</html>
样式如图:
这样就可以用键盘的 : W(上) D(右) S(下) A(左) ,控制箱子在网页上移动啦~
很有意思吧,哈哈