用js定义一个动态改变的一个元素的定位,然后用setInterval执行该函数为什么只执行一次?
<html><head><script>functionaa(){//document.getElementById('ul').style.position="rela...
<html>
<head>
<script>
function aa(){
//document.getElementById('ul').style.position="relative";
document.getElementById('ul').style.position="absolute";
document.getElementById('ul').style.left+="40px";
}
setInterval(aa,1000);
</script>
<style>
#div1{height: 350px;width: 100px;border: 1px solid black;overflow: hidden;}
#t0,#t1,#t2{height: 100px;width: 100px;background: pink;border: 1px solid red;}
</style>
</head>
<body>
<div id="div1">
<ul id="ul">
<li><div id="t0">1</div></li>
<li><div id="t1">2</div></li>
<li><div id="t2">3</div></li>
</ul>
</div>
</body>
</html> 展开
<head>
<script>
function aa(){
//document.getElementById('ul').style.position="relative";
document.getElementById('ul').style.position="absolute";
document.getElementById('ul').style.left+="40px";
}
setInterval(aa,1000);
</script>
<style>
#div1{height: 350px;width: 100px;border: 1px solid black;overflow: hidden;}
#t0,#t1,#t2{height: 100px;width: 100px;background: pink;border: 1px solid red;}
</style>
</head>
<body>
<div id="div1">
<ul id="ul">
<li><div id="t0">1</div></li>
<li><div id="t1">2</div></li>
<li><div id="t2">3</div></li>
</ul>
</div>
</body>
</html> 展开
1个回答
推荐于2016-10-12
展开全部
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<titel>
</titel>
<style>
ul{
position:absolute;
}
#div1 {
height: 350px;
width: 100px;
border: 1px solid black;
overflow: hidden;
}
#t0,#t1,#t2 {
height: 100px;
width: 100px;
background: pink;
border: 1px solid red;
}
</style>
<script>
function aa() {
var ul = document.getElementById('ul');
var lef = parseFloat(ul.style.left) || ul.offsetLeft;
lef += 40;
ul.style.left = lef + "px";
}
setInterval(aa, 1000);
</script>
</head>
<body>
<div id="div1">
<ul id="ul">
<li>
<div id="t0">
1
</div>
</li>
<li>
<div id="t1">
2
</div>
</li>
<li>
<div id="t2">
3
</div>
</li>
</ul>
</div>
</body>
</html>
追问
var lef = parseFloat(ul.style.left) || ul.offsetLeft;
这局是做兼容性吗?之前试过lef = parseFloat(ul.style.left)差不多的代码好像没用。然后是了下lef = ul.offsetLeft;那个ul就能动
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询