<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="ad1" style="width:200px;height:200px;background-image:url(4.jpg)" >
<a href="#" style="width:200px;height:200px;">这是一个链接</a>
</div>
<script type="text/javascript">
var x = 50, y = 60
var xin = true, yin = true
var step = 1
var delay = 10
var obj = document.getElementById("ad1")
function FloatAD1() {
var L = T = 0
var R = document.documentElement.clientWidth - obj.offsetWidth
var B = document.documentElement.clientHeight - obj.offsetHeight
obj.style.left = (x + document.documentElement.scrollLeft) + "px";
obj.style.top = y + document.documentElement.scrollTop + "px";
x = x + step * (xin ? 1 : -1);
if (x < L) { xin = true; x = L }
if (x > R) { xin = false; x = R }
y = y + step * (yin ? 1 : -1);
if (y < T) { yin = true; y = T }
if (y > B) { yin = false; y = B }
}
var itl = setInterval("FloatAD1", delay)
obj.onmouseover = function () { clearInterval(itl) }
obj.onmouseout = function () { itl = setInterval("FloatAD1", delay) }
</script>
</form>
</body>
</html>
1)要absolute定位
<div id="ad1" style="width:200px;height:200px;background-image:url(4.jpg);position:absolute">
<a href="#" style="width:200px;height:200px;">这是一个链接</a>
</div>
2)执行函数要加()
var itl = setInterval("FloatAD1()", delay)