6月 17

支持w3c标准的JS漂浮代码 晴

yaiba , 11:10 , 技术运用 , 评论(0) , 引用(0) , 阅读(524) , Via 本站原创
昨天美工在那弄js的漂浮,唠唠叨叨的,后来我决定帮他看看。记得javascript漂浮的代码满网都是,于是顺手去网上摘了几段回来用了,可是没想到哦那个嫁接出来的代码居然不支持W3C标准,有的方法居然无法侦测到浏览器的下边缘坐标,漂浮代码失效的原因如下:
在使用以下声明时,会使一个最常用的漂浮下拉广告失效。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona...
<html xmlns="http://www.w3.org/1999/xhtml">

直接导致document.body.scrollTop永远等于0,body Scroll永远也不会引发。
所以我把document.body用document.documentElement替换掉,结果就好用了。
完整的符合W3C标准的js漂浮代码如下:
HTML部分
<DIV id=img1 style="Z-INDEX: 100; LEFT: 2px; WIDTH: 120px; POSITION: absolute; TOP: 43px; HEIGHT: 70px; visibility: visible;">
  <a href="http://2008.cga.com.cn" target="_blank"><img src="{dede:field name='templeturl'/}/v200805/images/pic6.15.gif" width="120" height="70" border="0"></a>
<table width="40" border="0" align="right" cellpadding="0" cellspacing="0">
  <tr>
    <td width="40" align="center"><a href="#" onClick="javascript:document.getElementById('img1').style.visibility='hidden';" style="font-size:12px; color:#CC3300; text-decoration:none;">关 闭</a></td>
  </tr>
</table>
</DIV>
<SCRIPT src="{dede:field name='templeturl'/}/v200805/js/js.js"></SCRIPT>

JavaScript部分,另存为js.js

var xPos = 300;
var yPos = 200;
var step = 1;
var delay = 30;
var height = 0;
var Hoffset = 0;
var Woffset = 0;
var yon = 0;
var xon = 0;
var pause = true;
var interval;
img1.style.top = yPos;
function changePos()
{
width = document.documentElement.clientWidth;
height = document.documentElement.clientHeight;
Hoffset = img1.offsetHeight;
Woffset = img1.offsetWidth;
img1.style.left = xPos + document.documentElement.scrollLeft;
img1.style.top = yPos + document.documentElement.scrollTop;
if (yon)
  {yPos = yPos + step;}
else
  {yPos = yPos - step;}
if (yPos < 0)
  {yon = 1;yPos = 0;}
if (yPos >= (height - Hoffset))
  {yon = 0;yPos = (height - Hoffset);}
if (xon)
  {xPos = xPos + step;}
else
  {xPos = xPos - step;}
if (xPos < 0)
  {xon = 1;xPos = 0;}
if (xPos >= (width - Woffset))
  {xon = 0;xPos = (width - Woffset);   }
}

function start()
{
  img1.visibility = "visible";
  interval = setInterval('changePos()', delay);
}
function pause_resume()
{
  if(pause)
  {
    clearInterval(interval);
    pause = false;}
  else
  {
    interval = setInterval('changePos()',delay);
    pause = true;
    }
  }
start();


Tags: , ,
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]