利用指令碼封裝,方便實現網站懸浮廣告

來源:互聯網
上載者:User
封裝|廣告|指令碼  

下面是JS指令碼(floatdiv.js)

/*======================================================================
  浮動塊支援指令碼
  DESIGN BY :  彭國輝
  DATE:    2004-02-26
  SITE:    http://kacarton.yeah.net/
  EMAIL:     kacarton@sohu.com
======================================================================*/

var alLeft = 0, alCenter = 1, alRight  = 2;   //水平對齊
var alTop  = 0, alMiddle = 1, alBottom = 2;   //垂直對齊

var f = new Array();
var cnt = 0;

function floatDiv(objId, align, vAlign, x, y, smooth)
{
  /*浮動塊對象
    資料結構:
    objId: 對象ID
    align: 水平對齊,枚舉型
    vAlign:垂直對齊,枚舉型內容
    x, y:  xy座標,整型
    smooth:平滑移動,布爾型
  */
  this.objId  = objId;
  this.align  = align;
  this.vAlign = vAlign;
  this.x = x;
  this.y = y;
  this.smooth = smooth;
}

function addDiv(objId, align, vAlign, x, y, smooth)
{   //添加浮動塊
  f[cnt++] = new floatDiv(objId, align, vAlign, x, y, smooth);
}

function floatMove()
{   //重新置放各塊位置
  var objX, objY;
  if (document.all)
  {
    var d = document.body;
//status = "scrollLeft:"+f[0].objId.style.pixelTop+"| offsetWidth:"+d.offsetWidth;
    for (var i=0; i<f.length; i++)
    {
      switch (f[i].align)
      {
        case alLeft  : objX = d.scrollLeft + f[i].x; break;
        case alCenter: objX = d.scrollLeft + Math.ceil((d.offsetWidth - f[i].objId.offsetWidth)/2) + f[i].x; break;
        case alRight : objX = d.scrollLeft + d.offsetWidth - f[i].x - f[i].objId.offsetWidth - 20; break; //減捲軸20
      }
      switch (f[i].vAlign)
      {
        case alTop   : objY = d.scrollTop + f[i].y; break;
        case alMiddle: objY = d.scrollTop + Math.ceil((d.offsetHeight - f[i].objId.offsetHeight)/2) + f[i].y; break;
        case alBottom: objY = d.scrollTop + d.offsetHeight - f[i].y - f[i].objId.offsetHeight; break;
      }
      if (f[i].smooth) smoothMove(f[i].objId, objX, objY);
      else
      {
        f[i].objId.style.pixelLeft = objX;
        f[i].objId.style.pixelTop  = objY;
      }
    }
  }
  
  if (document.layers)
  {
    for (var i=0; i<f.length; i++)
    {
      var objId = f[i].objId;
      switch (f[i].align)
      {
        case alLeft  : objX = pageXOffset + f[i].x; break;
        case alCenter: objX = pageXOffset + Math.ceil((window.innerWidth-objId.width)/2) + f[i].x; break;
        case alRight : objX = pageXOffset + window.innerWidth - f[i].x - document.objId.width; break;
      }
      switch (f[i].vAlign)
      {
        case alTop   : objY = pageYOffset + f[i].y; break;
        case alMiddle: objY = pageYOffset + Math.ceil(window.innerHeight/2) + f[i].y; break;
        case alBottom: objY = pageYOffset + window.innerHeight - f[i].y - document.objId.height; break;
      }
      if (t[i].smooth) smoothMove(f[i].objId, objX, objY);
      else
      {
        document.objId.left = objX;
        document.objId.top  = objY;
      }
    }
  }
  
  setTimeout("floatMove();",50);
}

function smoothMove(obj, x, y)
{
  var percent;
  if (document.all)
  {
    percent = .1 * (x - obj.style.pixelLeft);
    if (percent > 0) percent = Math.ceil(percent); else percent = Math.floor(percent);
    obj.style.pixelLeft += percent;
    
    percent = .1 * (y - obj.style.pixelTop);
    if (percent > 0) percent = Math.ceil(percent); else percent = Math.floor(percent);
    obj.style.pixelTop += percent;
  }
  if (document.layers)
  {
    percent = .1 * (x - document.objId.left);
    if (percent > 0) percent = Math.ceil(percent); else percent = Math.floor(percent);
    document.objId.left += percent;
    
    percent = .1 * (y - document.objId.top);
    if (percent > 0) percent = Math.ceil(percent); else percent = Math.floor(percent);
    document.objId.top += percent;
  }
}


setTimeout("floatMove();",50);

調用例子:

<script language="javascript" src="lib/floatdiv.js"></script>
<script language="javascript">
if (navigator.appName == "Netscape")
{
  document.write("<layer id=ad_hr top=300 width=100 height=120><a href=http://www.fpc.org.cn/hr/040522.htm target=_blank><img src='http://www.webjx.com/htmldata/2005-02-25/ad/hr40522.gif' width=120 height=180 border=0></a></layer>");
}
else
{
  document.write("<div id=ad_hr style='position: absolute;width:120;top:300;left:5;visibility: visible;z-index: 1'><a href=http://www.fpc.org.cn/hr/040522.htm target=_blank><img src='http://www.webjx.com/htmldata/2005-02-25/ad/hr40522.gif' width=120 height=180 border=0></a></div>");
}
addDiv(ad_hr, alRight, alTop, 0, 30, true);
</script>



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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