<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
*{margin:0;padding:0;}
#box{width:235px;height:100px;position:absolute;left:-202px;}
#div1{width:200px;height:100px;background:#ccc;border:1px solid #000;float:left;}
#div2{width:30px;height:60px;background:orange;border:1px solid #000;
margin-left:200px;margin-top:20px;border-left:-none;}
</style>
<script>
window.onload=function()
{
var oBox=document.getElementById("box");
var oDiv1=document.getElementById("div1");
var oDiv2=document.getElementById("div2");
var oTimer=null;
oBox.onmouseover=function()
{ clearInterval(oTimer);
oTimer=setInterval(function(){
oBox.style.left=oBox.offsetLeft+10+"px";
if (oBox.style.left>0+"px")
{
oBox.style.left=0+"px";
}},10);
}
oBox.onmouseout=function()
{ clearInterval(oTimer);
oTimer=setInterval(function(){
oBox.style.left=oBox.offsetLeft-10+"px";
if (oBox.style.left>-202+"px") //A
{
oBox.style.left=-202+"px";
}
},10);
}
}
</script>
</head>
<body>
<div id="box">
<div id="div1"></div>
<div id="div2"><br>滑<br>出</div>
</div>
</body>
</html>A处 :想让DIV向左滑动时不超出-202.如果用小于-202时会一直向左移动超出了-202也不会停,只能用大于-202时才能达到想要的要求。但有个问题,就是当鼠标移入时滑出。移出时滑入。可滑入时太快,不像是按-10的要求在滑动,而是直接跳的。
不知道是思路错了,还是left为负数不能设定范围。菜单滑出好像没什么问题。
<html>
<head>
<style type="text/css">
*{margin:0;padding:0;}
#box{width:235px;height:100px;position:absolute;left:-202px;}
#div1{width:200px;height:100px;background:#ccc;border:1px solid #000;float:left;}
#div2{width:30px;height:60px;background:orange;border:1px solid #000;
margin-left:200px;margin-top:20px;border-left:-none;}
</style>
<script>
window.onload=function()
{
var oBox=document.getElementById("box");
var oDiv1=document.getElementById("div1");
var oDiv2=document.getElementById("div2");
var oTimer=null;
oBox.onmouseover=function()
{ clearInterval(oTimer);
oTimer=setInterval(function(){
oBox.style.left=oBox.offsetLeft+10+"px";
if (oBox.style.left>0+"px")
{
oBox.style.left=0+"px";
}},10);
}
oBox.onmouseout=function()
{ clearInterval(oTimer);
oTimer=setInterval(function(){
oBox.style.left=oBox.offsetLeft-10+"px";
if (oBox.style.left>-202+"px") //A
{
oBox.style.left=-202+"px";
}
},10);
}
}
</script>
</head>
<body>
<div id="box">
<div id="div1"></div>
<div id="div2"><br>滑<br>出</div>
</div>
</body>
</html>A处 :想让DIV向左滑动时不超出-202.如果用小于-202时会一直向左移动超出了-202也不会停,只能用大于-202时才能达到想要的要求。但有个问题,就是当鼠标移入时滑出。移出时滑入。可滑入时太快,不像是按-10的要求在滑动,而是直接跳的。
不知道是思路错了,还是left为负数不能设定范围。菜单滑出好像没什么问题。
if (parseInt(oBox.style.left,10)>-202) // 用int比较,不用用字符串比较
字符串 "21" 大于 "202"