javascript - 关于一个飘浮去效果的小小疑惑(下面两个注释)
天蓬老师
天蓬老师 2017-04-10 14:53:05
[JavaScript讨论组]
html<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
*{margin:0px; margin:0px;}
body{background:#8a234e url(bg.png) no-repeat center 0;}
#p1{position:relative; width:500px; height:260px;  margin:146px auto 0; overflow:hidden; }
#p1 a{ position:absolute; color:#000; text-decoration:none; top:260px; display:block; border:solid 1px #000; background:#fff; filter:alpha(opacity:30); opacity:0.3; font-size:14px; padding:3px 5px; font-family:arial;}
#p1 a:hover { filter:alpha(opacity:100); opacity:1; font-weight:bold; font-size:16px; }
</style>


<script type="text/javascript">
window.onload=function()
{
    var op=document.getElementById('p1');
    var aA=document.getElementsByTagName('a');
    var i=0;
    for(i=0;i<aA.length;i++)
    {
        aA[i].pause=1;
        aA[i].time=null;
        initialize(aA[i]);
        aA[i].onmouseover=function()
        {
            this.pause=0;    
        };
        aA[i].onmouseout=function()
        {
            this.pause=1;
        };
    }
    setInterval(starmove,24);
    function starmove()
    {
        for(i=0;i<aA.length;i++)
        {
            if(aA[i].pause)
            {
                domove(aA[i]);
            }
        }
    }
    function domove(obj)
    {
        if(obj.offsetTop<=-obj.offsetHeight)
        {
            obj.style.top=op.offsetHeight+"px";
            initialize(obj);
        }
        else
        {
            obj.style.top=obj.offsetTop-obj.ispeed+"px";    
        }
    }
    function initialize(obj)
    {
        var iLeft=parseInt(Math.random()*op.offsetWidth);
        var scale=Math.random()*1+1;
        var iTimer=parseInt(Math.random()*1500);
       //这里为什么要设置obj.pause=0,不设置好像也没有关系吧?
        obj.pause=0;

        obj.style.fontSize=12*scale+'px';

        if((iLeft-obj.offsetWidth)>0)
        {
            obj.style.left=iLeft-obj.offsetWidth+"px";
        }
        else
        {
            obj.style.left=iLeft+"px";
        }
        clearTimeout(obj.time);
         //这里为什么要运定时器,不用不一样可以运行么?这个作用是什么?
        obj.time=setTimeout(function(){
            obj.pause=1;
        },iTimer);
        obj.ispeed=Math.ceil(Math.random()*4)+1;
    }
};
</script>


</head>
<body>


<p id="p1">
    <a href="#" target="_blank">JS课程</a>
    <a href="#" target="_blank">教程</a>
    <a href="#" target="_blank">试听</a>
    <a href="#" target="_blank">精品</a>
</p>


</body>
</html>
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
迷茫
//意思是移动的元素到达视窗以外之后 starmove 函数中 `if(aA[i].pause)`中为false.
obj.pause = 0;

 /*设定多少时间后将obj.pause = 1;即 starmove 函数中 `if(aA[i].pause)`中为true.
   不能将其删除不然 domove 函数不能执行*/
obj.time=setTimeout(function(){
        obj.pause=1;
},iTimer);

obj.pause 为自定义属性.
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号