why正方向和負(fù)方向值不同才執(zhí)行代碼
為什么我的代碼和老師一樣,但是執(zhí)行不了。正方向我的是+1,負(fù)方向我是-1就不行,除非負(fù)方向我是-10才行,這是為什么,width和height我設(shè)置得和老師一樣啊
<!doctype>
<html>
<head>
</head>
<style type="text/css">
#div1
{
width:200px;
height:200px;
background:red;
position:relative;
top:0px;
left:-200px;
}
#div1 span
{width:20px;
height:50px;
background:blue;
position:absolute;
top:75px;
left:200px;
}
</style>
<script type="text/javascript">
window.onload=function()
{var odiv=document.getElementById("div1");
odiv.onmouseover=function()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? startmove();
? }
odiv.onmouseout=function()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? startmove1();
? }
}
var timer=null;
function startmove()
? ? ? ? ? ? ? ? ? ? ? ? ? {clearInterval(timer);
? var odiv1=document.getElementById("div1");?
? timer=setInterval(function()
? ? ? ? ? ? ? ? ? ? ? ? ? ?{
if(odiv1.offsetLeft>=0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
clearInterval(timer);
}
? ? ? ? ? ? ? ? ? ? ? ? ?else{
?odiv1.style.left=odiv1.offsetLeft+1+'px';
}?
},20)
?}
?
?function startmove1()
? ? ? ? ? ? ? ? ? ? ? ? ? {clearInterval(timer);
? var odiv1=document.getElementById("div1");?
? timer=setInterval(function()
? ? ? ? ? ? ? ? ? ? ? ? ? ?{
if(odiv1.offsetLeft<=-200)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{?
clearInterval(timer);
}
? ? ? ? ? ? ? ? ? ? ? ? ?else{
? ? ? ? ? ? ?
?odiv1.style.left=odiv1.offsetLeft-10+'px';
}?
},30)
?}
?
</script>
<body>
<div id="div1">
? ? ? ? <span id="share">分享</span>
</div>
</body>
</html>
2016-06-13
還有一般老師講的代碼在style的第一行都會(huì)加上*{padding:0;margin:0}這行代碼的,建議你以后也用上
2016-06-13
不知道為什么運(yùn)行你這個(gè)程序,會(huì)出現(xiàn)body的margin值,這樣div的實(shí)際的offsetLeft值跟預(yù)期的有差別。你在style的第一行前面加上 ?*{margin:0px;} 就可以了
2016-06-13
樣式里面 ?加個(gè)*{ margin:0; padding:0;}