Javascript and onMouseOver

Javascript and onMouseOver

本文关键字:onMouseOver and Javascript      更新时间:2023-09-26

我创建了一个脚本,其中包含一个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>

感谢您的回复!

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

注意:您的代码中有错误

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

http://jsfiddle.net/2r9usf4h/