2 cl yq cl_yq 于 2016.09.14 14:59 提问

漂浮漂不起来······

图片说明

 <%@ 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个回答

showbo
showbo   Ds   Rxr 2016.09.14 15:05
已采纳

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)
cl_yq
cl_yq ~~~谢谢
大约一年之前 回复
showbo
showbo 回复cl_yq: 默认是static定位,设置left/top无效。需要absolute/relative/fixed定位才有效。可以看这个:http://www.w3school.com.cn/cssref/pr_class_position.asp
大约一年之前 回复
cl_yq
cl_yq 为什么非要绝对定位一下?
大约一年之前 回复
showbo
showbo 或者直接句柄 setInterval(FloatAD1, delay)。字符串要是js代码。 obj.onmouseout = function () { itl = setInterval("FloatAD1", delay) }这个也要记得改
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片