js之漂浮广告效果

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #box{width:150px;height:200px;background:url(pic.jpg) no-repeat;
                position:absolute;left:0;top:0;}
        </style>
        <script type="text/javascript">
            window.onload=function(){
                var box=document.getElementById('box');
                var close=document.getElementById('close');
                
                //获取可是区域的高度和宽度
                var win_height=document.documentElement.clientHeight;
                var win_width=document.documentElement.clientWidth;
                
                //计算元素可到达最大的高度和宽度
                var max_top=win_height-box.offsetHeight;
                var max_left=win_width-box.offsetWidth;
                
                var x=1;y=1;
                console.log(max_top);
                
                //设置定时器调用的函数
                function run(){ //格式不要写错
                    //获取旧的top和left
                    var old_left=box.offsetLeft;
                    var old_top=box.offsetTop;
                    
                    //计算新的top和left
                    var new_left=old_left+x;
                    var new_top=old_top+y;
                    
                    //将新值赋值回去
                    box.style.left=new_left+'px';
                    box.style.top=new_top+'px';
                    
                    if(new_top==max_top){
                        y=-1;
                    }
                    if(new_top==0){
                        y=1;
                    }
                    if(new_left==max_left){
                        x=-1;
                    }
                    if(new_left==0){
                        x=1;
                    }
                }
                //函数结束
                //设置全局定时器
                var timer=setInterval(run,40);  //40毫秒走一次
                
                box.onmouseover=function(){
                    clearInterval(timer);//鼠标移入,停止定时器
                }
                
                box.onmouseout=function(){
                    //恢复定时器
                    timer=setInterval(run,40);
                }
                
                close.onclick=function(){
                    box.style.display='none'; //点击关闭安装,隐藏广告
                }
            }
        </script>
    </head>
    <body>
        <div id="box">
            <img src="shut.jpg" id="close"/>
        </div>
    </body>
</html>

 

转载于:https://www.cnblogs.com/youbiao/p/7088688.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值