我希望如果鼠标2秒不动(或者点下左键),则出现一个层,里面显示鼠标在当前iframe里的坐标(单位px),这个效果能做吗?

解决方案 »

  1.   

    当然可以,onload事件中加个方法var iTimeoutId = setTimeout("show()", 2000);function show() {
    //  显示坐标
    }onmousemove事件中,clearTimeout(iTimeoutId);iTimeoutId = setTimeout("show()", 2000);
      

  2.   

    《professional javascript》 JavaScript in the Browser -》Intervals and timeouts
    有详细论述,还是老外牛啊
      

  3.   

    setTimeout 尽量少用,容易造成内存泄露。
    用setInterval吧
      

  4.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <script>
    var iTimeoutId;function show(){
    alert('ok');
    iTimeoutId = setTimeout('show()', 2000);
    }function wait() {
    clearTimeout(iTimeoutId);
    iTimeoutId = setTimeout('show()', 2000);
    }
    </script> 
    </head>
    <body style="border:1px solid;width:1024px;height:900px;" onload="iTimeoutId = setTimeout('show()', 2000);" onmousemove="wait();">
     在方形区域内有效
    </body>
    </html>用setInterval,也可,但是setTimeout 尽量少用,容易造成内存泄露,不清楚,不知是哪儿看见这个报道,
    另,鼠标在当前iframe里的坐标,这个会吧,
    另,我懒得在window下加事件了,只在document.body下加,这个lz会吧
      

  5.   

    setTimeout 是无限递归调用,使用setTimeout后,网页开的时间长了,你会发现卡得很郁闷,谢谢。
      

  6.   

    ie6 firefox下测试通过<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title></title>
        <script type="text/javascript">        window.onload = function()
            {
                document.documentElement.onclick = function()
                { document.getElementById("layer").style.display = "none"; }
            }
            
        </script>
    </head>
    <body>
    <div id="layer" style="padding:4px;border:1px solid #ccc;display:none"></div>
    <iframe src="index.aspx" />
    </body>
    </html>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
        <script type="text/javascript">        window.onload = function()
            {
                document.documentElement.onclick = function()
                {
                    var oEvent = arguments[0] || window.event;
                    var layer = parent.document.getElementById("layer");
                    layer.style.display = "block";
                    layer.innerHTML = "x:" + oEvent.clientX + ",y:" + oEvent.clientY;
                }
            }
            
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          
        </div>
        </form>
    </body>
    </html>
      

  7.   


    这代码,如果鼠标移动到iframe外面也会触发这个事件
      

  8.   


    这个可以,但是我想了下,因为页面还是其他的onclick时间,可以会和这些事件冲突,而让用户觉得不舒服,能不能做到鼠标停留x秒之后触发,或者设置个快捷键来触发?
      

  9.   

    实际上我只是在html元素上中了onclick事件来关闭显示的层.你完全可以将这段代码的事件处理函数转移到一个button上.稍微修改一下即可.