我试图在Javascript中创建一个震动游戏的图像,你可以摇动你选择的图像,只是简单地获得积分。如果你在没有图像的情况下摇动鼠标 - 它可以工作,但是如果你这样做的话它与图像 - 它没有。真的需要你的帮助。
<script type="text/javascript">
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;
function movemouse(e)
{
if (isdrag)
{
dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
dobj.style.top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
return false;
}
}
function selectmouse(e)
{
var fobj = nn6 ? e.target : event.srcElement;
var topelement = nn6 ? "HTML" : "BODY";
while (fobj.tagName != topelement && fobj.className != "dragme")
{
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
if (fobj.className=="dragme")
{
isdrag = true;
dobj = fobj;
tx = parseInt(dobj.style.left+0,10);
ty = parseInt(dobj.style.top+0,10);
x = nn6 ? e.clientX : event.clientX;
y = nn6 ? e.clientY : event.clientY;
document.onmousemove=movemouse;
return false;
}
}
document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");
</script>
<style type="text/css">
.dragme{position:relative;}
</style>
<table><tr><td align="center">
<img src="./dirt.png" width="150" height="150" alt="drag me on the canvas" style="cursor:move;" title="shake" class="dragme">
</td></tr></table>
<form name='score'><input name='scorer' type='text' value='0' style='border:none; font-size:22pt; text-align:center;' readonly></form><p>
<p id="scorearea"><canvas style="font-size:32pt;width:400px;height:300px;border:1px solid #000000;" onmousemove=document.score.scorer.value++></canvas></p>