回 帖 发 新 帖 刷新版面

主题:简单漂浮广告设计的问题~

<html>
<head>
<title>无标题文档</title>
<script language="javascript">
function move()
{
    var w = window.document.body.offsetWidth;
    var h = window.document.body.offsetHeight;
    
    var xSpeed = 20;
    var ySpeed = Math.tan(Math.PI/4)*xSpeed;
    var x = document.getElementById("Layer1").style.left;
    var y = document.getElementById("Layer1").style.top;
    if(parseInt(x)+150>w|| parseInt(x)<0)
    {
        xSpeed = -xSpeed;
    }
    if(parseInt(y)+100>h||parseInt(y)<0)
    {
        ySpeed = -ySpeed;
    }
    document.getElementById("Layer1").style.left=parseInt(x)+xSpeed;
    document.getElementById("Layer1").style.top=parseInt(y)+ySpeed;
    setTimeout("move()",800);
}
</script>
<style type="text/css">
<!--
#Layer1 {
    position:absolute;
    left:0px;
    top:0px;
    width:150px;
    height:100px;
    z-index:1;
}
-->
</style>
</head>

<body onLoad="move()">
<div id="Layer1"><img src="image/0E91FBEBB0E7B14D0A0C9714F22A9491.gif" width="150" height="100"></div>
</body>
</html>
我插入的这张图片页面加载后根本不动,高手帮看看错在哪里?

回复列表 (共1个回复)

沙发

style.left和style.top均是XXpx的形式,是不能通过parseInt转换成为像素值(数字)的。自己写个函数或者用第三方库来解决这个问题吧。
另外要注意跨浏览器的兼容性。IE已经提供leftPixel之类的返回数值的函数,不过其它的浏览器没有。

我来回复

您尚未登录,请登录后再回复。点此登录或注册