<!-- 一???的?? -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>一???的????</title>
<style>
#divAdvert {
position: absolute;
font: 20pt Arial;
color:mediumspringgreen;
background-color:darkgrey;
left: 0;
top: 10px;
}
</style>
</head>
<body>
<div id="divAdvert">
?里是??的文字?容.
</div>
<script>
var switchDirection = false;
function doAnimation()
{
var divAdvertisement = document.getElementById("divAdvert");
var currentLeft = divAdvertisement.offsetLeft;
var newLocation; if (!switchDirection)
{
newLocation = currentLeft + 2;
if (currentLeft >= 1080)
{
switchDirection = true;
}
}
else
{
newLocation = currentLeft - 2;
if (currentLeft <= 0)
{
switchDirection = false;
}
}
divAdvertisement.style.left = newLocation + "px";
}
setInterval(doAnimation, 10);
</script>
</body>
</html>和<!-- 一???的?? -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>一???的????</title>
<style>
#divAdvert {
position: absolute;
font: 20pt Arial;
color:mediumspringgreen;
background-color:darkgrey;
left: 0;
top: 10px;
}
</style>
</head>
<body>
<div id="divAdvert">
?里是??的文字?容.
</div>
<script>
var switchDirection = false;
function doAnimation()
{
var divAdvertisement = document.getElementById("divAdvert");
var currentLeft = divAdvertisement.offsetLeft;
var newLocation;
var switchDirection = false;
if (!switchDirection)
{
newLocation = currentLeft + 2;
if (currentLeft >= 1080)
{
switchDirection = true;
}
}
else
{
newLocation = currentLeft - 2;
if (currentLeft <= 0)
{
switchDirection = false;
}
}
divAdvertisement.style.left = newLocation + "px";
}
setInterval(doAnimation, 10);
</script>
</body>
</html>一个局部变量与全局变量的区别导致的动画不会往左移动的小bug,求问这个bug怎么导致的?
{
switchDirection = true;
}这种成立才会赋值推荐学习资料
JavaScript/Ajax开发技巧
JavaScript apply与call的用法及区别