码迷,mamicode.com
首页 > 其他好文 > 详细

图片漂浮

时间:2017-11-02 23:11:56      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:pad   int   function   span   rip   onload   div   etl   src   

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>图片漂浮</title>
    <style type="text/css">
        *{margin:0;padding: 0;}
        #box{
            position: absolute;
            top: 0px;
            left: 0px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var box=document.getElementById("box");
            var time=null,x= 1,y= 1,speed=5;
            function go(){
                //水平方向
                if((box.offsetLeft+box.offsetWidth)>document.documentElement.clientWidth){
                    x=-1;
                }
                if(box.offsetLeft<0){
                    x=1;
                }
                box.style.left=box.offsetLeft+x*speed+"px";
                //垂直方向
                if((box.offsetTop+box.offsetHeight)>document.documentElement.clientHeight){
                    y=-1;
                }
                if(box.offsetTop<0){
                    y=1;
                }
                box.style.top=box.offsetTop+x*speed+"px";
            }
            //定时函数
            time=setInterval(go,100);
            //鼠标移上去 停止
            box.onmousemove=function(){
                if(time!=null){
                    clearInterval(time);
                }
            }
            //鼠标离开  继续移动
            box.onmouseout=function(){
                time=setInterval(go,100);
            }
        }
    </script>
</head>
<body>
<div id="box">
    <img src="../images/cat.jpg" height="200px" width="200px">
</div>
</body>
</html>

 

图片漂浮

标签:pad   int   function   span   rip   onload   div   etl   src   

原文地址:http://www.cnblogs.com/zwy0709/p/7774899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!