永发信息网

控制DIV层在制定的像素内移动

答案:2  悬赏:20  手机版
解决时间 2021-08-22 15:35
  • 提问者网友:無理詩人
  • 2021-08-22 08:00
 <script type="text/javascript" language="javascript">
       
        function move_div() {
            var mydiv = document.getElementById("myDiv");
            var currently_div=mydiv.style.pixelTop;          
            var whether = false;
            if (currently_div == 200) {
                whether = true;
                alert(whether);
            } else if (currently_div == 1) {
                whether = false;
                alert(whether);
            }
            if (whether == false) {
                mydiv.style.pixelTop += 1;
                currently_div++
            } else if (whether == true) {
                mydiv.style.pixelTop -= 1;
                currently_div--;
            }   
        }
        setInterval("move_div()", 10);
    </script>

我是初学者也许方法很笨,但希望能帮解决下,谢谢

最佳答案
  • 五星知识达人网友:街头电车
  • 2021-08-22 08:56

给你一个我曾经自己写的小效果吧,参考一下。


<!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>
<script type="text/javascript">
function moveBox(){
 var box=document.getElementById("box");
 var ch=(document.documentElement.scrollTop+document.documentElement.clientHeight-box.offsetHeight);
 var cw=(document.documentElement.scrollLeft+document.documentElement.clientWidth-box.offsetWidth);
 var top=parseInt(box.style.top);
 var left=parseInt(box.style.left);
 var speed=1;
 //alert(document.getElementById("box").style.top);
 if(top<ch && left==0){
  top+=speed;
  box.style.top=top;
 }
 else if(top==ch && left<cw){
  left+=speed;
  box.style.left=left;
 }
 else if(top<=ch && top>0 && left<=cw){
  top-=speed;
  box.style.top=top;
 }
 else if(top==0 && left<=cw){
  left-=speed;
  box.style.left=left;
 }
 //if(top==ch && left==cw){clearInterval(t);}
 document.getElementById("zb").innerHTML="X:"+left+",Y:"+top;
 //box.style.borderColor="rgb("+Math.random()*100+","+Math.random()*200+","+Math.random()*20+")";
 box.style.borderColor="rgb("+top+","+left+","+Math.random()*20+")";
}
var t;
function startMove(){
 t=setInterval("moveBox()",1);
}
function stopMove(){
 clearInterval(t);
}
 
</script>
</head>
<body onload="startMove()">
<div class="box" id="box" style="position:absolute;top:0px;left:0px;border:2px solid green;background:#CCCCCC;text-align:center;">
 <div id="zb" style="line-height:100px;width:100px;" onmouseover="stopMove()" onmouseout="startMove()"></div>
</div>


</body>
</html>

全部回答
  • 1楼网友:想偏头吻你
  • 2021-08-22 09:05

可以用swich ……case实现啊

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯