为什么我的代码运行起来会不停的闪烁,无法正确运行,求帮助

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>my运动</title>

<!-- <link href="运动.css" type="text/css"  rel="stylesheet"/>

<script src="运动.js" type="text/javascript"></script> -->

<style type="text/css">

*{ 

margin:0; 

padding:0;

font-family:"宋体";

}

#myblock

{

width:200px;

height:200px;

background:#99CC33;

position:relative;

left:-200px;

top:0;

}

#myblock span

{

width:50px;

height:50px;

position:absolute;

left:200px;

top:150px;

background:#9900FF;

}

</style>

<script type="text/javascript">

function  mouseOver(L){

var timer=null;

clearInterval(timer);

var adiv=document.getElementById("myblock");

timer= setInterval(function(){

var v=0;

if(adiv.offsetLeft>L){

v=-10;

}

else{

v=10;

}

if(adiv.offsetLeft==L){

clearInterval(timer);

}

else{

adiv.style.left=adiv.offsetLeft+v+'px';

}

},30);

}

</script>


</head>

<body>

<div id="myblock"  onmouseover="mouseOver(0)" onmouseout="mouseOver(-200)" >

<span >一个</span>

</div>

</body>

</html>


隔壁诸葛村夫
浏览 1398回答 1
1回答

皓韵儿

你把  var timer=null;  设为全局变量, 要不然,你每次都会开一个计时器,所以会闪烁, 望采纳,谢谢
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript