javascript浮動廣告的實現

來源:互聯網
上載者:User

<script>
var Rimifon = {
    "Ads" : new Object,
    "NewFloatAd" : function(imgUrl, strLink)
    {
        var ad = document.createElement("a");
        ad.DirV = true;
        ad.DirH = true;
        ad.AutoMove = true;
        ad.Image = new Image;
        ad.Seed = Math.random();
        ad.Timer = setInterval("Rimifon.Float(" + ad.Seed + ")", 50);
        this.Ads[ad.Seed] = ad;
        ad.Image.Parent = ad;
        ad.style.position = "absolute";
        ad.style.left = 0;
        ad.style.top = 0;
        ad.Image.src = imgUrl;
        ad.Image.onmouseover = function(){this.Parent.AutoMove = false;}
        ad.Image.onmouseout = function(){this.Parent.AutoMove = true;}
        if(strLink)
        {
            ad.href = strLink;
            ad.Image.border = 0;
            ad.target = "_blank";
        }
        ad.appendChild(ad.Image);
        document.body.appendChild(ad);
        return ad;
    },
    "Float" : function(floatId)
    {
        var ad = this.Ads[floatId];
        if(ad.AutoMove)
        {
            var curLeft = parseInt(ad.style.left);
            var curTop = parseInt(ad.style.top);
            if(ad.offsetWidth + curLeft > document.body.clientWidth + document.body.scrollLeft - 1)
            {
                curLeft = document.body.scrollLeft + document.body.clientWidth - ad.offsetWidth;
                ad.DirH = false;
            }
            if(ad.offsetHeight + curTop > document.body.clientHeight + document.body.scrollTop - 1)
            {
                curTop = document.body.scrollTop + document.body.clientHeight - ad.offsetHeight;
                ad.DirV = false;
            }
            if(curLeft < document.body.scrollLeft)
            {
                curLeft = document.body.scrollLeft;
                ad.DirH = true;
            }
            if(curTop < document.body.scrollTop)
            {
                curTop = document.body.scrollTop;
                ad.DirV = true;
            }
            ad.style.left = curLeft + (ad.DirH ? 1 : -1) + "px";
            ad.style.top = curTop + (ad.DirV ? 1 : -1) + "px";
        }
    }
}
</script><body><div style="width:30cm;height:40cm"></div></body><script>
Rimifon.NewFloatAd("http://www.zope.org/Members/Rimifon/NetDisk/FileStore/1147882113969.gif", "http://www.baidu.com");
var ad = Rimifon.NewFloatAd("http://www.fengyun.org/Images/Logo.jpg");
ad.style.left = 500;
ad.style.top = 456;
ad.Image.width = 88;
ad.Image.height = 31;
</script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.