Javascript和onMouseOver

时间:2015-03-14 13:21:21

标签: javascript eventhandler

我创建了一个脚本,它包含一个div容器。当我在容器上快速左右移动鼠标时,它不再起作用了。谷歌浏览器也不能正确处理它,为什么?

<html>
<head>
<title>move container</title>
<script type="text/javascript">
var position=0;
var interval;
var isRunning = false;

function moveRight0()
{
if (position => 20){

position = 19;

} 

if (isRunning==true){
isRunning = false;
return; 

} 

if (isRunning==false) { 

isRunning = true
document.getElementById("container0").style.left = position+"px";
position++;

if(position==20)
{ 
clearInterval(interval);
interval=0; 
}

}

}

function moveLeft0()
{
if (position < 1){

position = 0;

}


if (isRunning==true){
isRunning = false;
return; 
} 


if (isRunning==false) { 

isRunning = true
document.getElementById("container0").style.left = position+"px";
position--;
if(position<1)
{

clearInterval(interval);
interval = 0;
}

} 

}

function al(){
alert(interval);
}

function setIntervalLeft0()
{ 
interval = setInterval(moveLeft0,10);
}

function setIntervalRight0()
{ 
interval = setInterval(moveRight0,10);
}


</script>
</head>
<body>
<div onmouseover="setIntervalRight0()" onmouseout="setIntervalLeft0()" id="container0" style="width:50px; height:20px; position:absolute; z-index:1; top:0px; left:0px">text</div><br><br>
<input type="button" onclick="al()" name="zeige">
</body>
</html>

感谢您的回复!

1 个答案:

答案 0 :(得分:0)

我真的不明白你的代码应该做什么,但我做了一些小改动,它适用于chrome。

注意:您的代码中有错误

if (position => 20){
//=> is invalid, use >= instead

http://jsfiddle.net/2r9usf4h/