鼠标捕获(setCapture,releaseCapture)的学习

鼠标捕获(setCapture,releaseCapture)的学习鼠标捕获(setCapture)作用是将鼠标事件捕获到当前文档的指定的对象——对指定的对象设置鼠标捕获。这个对象会为当前应用程序或整个系统接收所有鼠标事件。所谓鼠标捕获,是指对鼠标事件(onmousedown,onmouseup,onmousemove,onclick,ondblclick,onmouseover,onmouseout)进行捕捉,使在容器内的子对象的鼠标事件均…

大家好,又见面了,我是你们的朋友全栈君。

鼠标捕获(setCapture)作用是将鼠标事件捕获到当前文档的指定的对象——对指定的对象设置鼠标捕获。这个对象会为当前应用程序或整个系统接收所有鼠标事件。

  • 所谓鼠标捕获,是指对鼠标事件(onmousedown, onmouseup, onmousemove, onclick, ondblclick, onmouseover, onmouseout)进行捕捉,使在容器内的子对象的鼠标事件均由容器对象触发,因此,只能在容器对象的鼠标事件函数中进行处理。
  • 当参数为true时,对鼠标进行捕捉,相反,不捕捉。
  • 与这个函数对应,releaseCapture方法释放鼠标捕获,并触发onlosecapture事件。

一、语法

1. MDN(Mozilla Developer Network)

element.setCapture(retargetToElement);

retargetToElement——If true, all events are targeted directly to this element; if false, events can also fire at descendants of this element.

document.releaseCapture()

释放鼠标捕捉——Once mouse capture is released, mouse events will no longer all be directed to the element on which capture is enabled.

2. msdn(Internet Explorer Dev Center

object.setCapture(containerCapture)

其中: containerCapture [in, optional]—— Type:
Boolean

  • true (true)——Default. 容器会捕获容器内所有对象的鼠标事件,即容器内的对象不会触发鼠标事件(跟容器外的对象一样)Events originating in a container are captured by the container.
  • false (false)——容器不会捕获容器内对象的鼠标事件,即容器内的对象可以正常地触发事件和取消冒泡。Events originating in a container are not captured by the container.

  object.setCapture() 当一个object的被 setCapture 后,他的方法将会被继承到整个文档进行捕获。当不需要把方法继承到整个文档捕获时,要用 object.releaseCapture() 来释放.

二、案例——简单拖拽

完整代码

<!DOCTYPE html>
<html>
<head>
<title>drag example</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<style type="text/css">
#drag{
    
    position:absolute;left:12px;top:24px;width:100px;height:150px; display:block;border:1px solid #000000;z-index:1;background:#eeeeee; cursor: pointer;}
</style>
</head>
<body>
<div id="drag">drag me</div>
<script type="text/javascript">
window.onload=function(){
    objDiv = document.getElementById("drag");
    drag(objDiv);
};
function drag(dv){
    dv.onmousedown=function(e){
        var d=document;
        e = e || window.event;

        var x= e.layerX || e.offsetX;
        var y= e.layerY || e.offsetY;

        //设置捕获范围
        if(dv.setCapture){
            dv.setCapture();
        }else if(window.captureEvents){
            window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
        }

        d.onmousemove=function(e){
            e= e || window.event;
            if(!e.pageX)e.pageX=e.clientX;
            if(!e.pageY)e.pageY=e.clientY;
            document.getElementById("drag").innerHTML= e.pageX+ e.pageY;
            var tx=e.pageX-x;
            var ty=e.pageY-y;
            dv.style.left=tx+"px";
            dv.style.top=ty+"px";
        };
        d.onmouseup=function(){
            //取消捕获范围
            if(dv.releaseCapture){
                dv.releaseCapture();
            }else if(window.captureEvents){
                window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
            }
            //清除事件
            d.onmousemove=null;
            d.onmouseup=null;
        };
    };
}
</script>
</body>
</html>

三、案例——完美拖拽

完整代码

鼠标捕获(setCapture,releaseCapture)的学习
鼠标捕获(setCapture,releaseCapture)的学习

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>完美拖拽</title>
<style type="text/css">
html,body{
     
     overflow:hidden;}
body,div,h2,p{
     
     margin:0;padding:0;}
body{
     
     color:#fff;background:#000;font:12px/2 Arial;}
p{
     
     padding:0 10px;margin-top:10px;}
span{
     
     color:#ff0;padding-left:5px;}
#box{
     
     position:absolute;width:300px;height:150px;background:#333;border:2px solid #ccc;top:50%;left:50%;margin:-75px 0 0 -150px;}
#box h2{
     
     height:25px;cursor:move;background:#222;border-bottom:2px solid #ccc;text-align:right;padding:0 10px;}
#box h2 a{
     
     color:#fff;font:12px/25px Arial;text-decoration:none;outline:none;}
</style>
<script type="text/javascript">
window.onload=function (){
    var oBox=document.getElementById("box");
    var oH2 = oBox.getElementsByTagName("h2")[0];
    var oA = oBox.getElementsByTagName("a")[0];
    var aSpan = oBox.getElementsByTagName("span");    
    var disX = disY = 0;
    var bDrag = false;
    var aPos = [{x:oBox.offsetLeft, y:oBox.offsetTop}];
    
    //鼠标按下, 激活拖拽
    oH2.onmousedown = function (event){        
        var event = event || window.event;
        bDrag = true;
        disX = event.clientX - oBox.offsetLeft;
        disY = event.clientY - oBox.offsetTop;        
        aPos.push({x:oBox.offsetLeft, y:oBox.offsetTop})
        
        this.setCapture && this.setCapture();        
        return false;
    };
    
    //拖拽开始
    document.onmousemove = function (event){
        if (!bDrag) return;
        var event = event || window.event;
        var iL = event.clientX - disX;
        var iT = event.clientY - disY;
        var maxL = document.documentElement.clientWidth - oBox.offsetWidth;
        var maxT = document.documentElement.clientHeight - oBox.offsetHeight;
        
        iL = iL < 0 ? 0 : iL;
        iL = iL > maxL ? maxL : iL; 
        
        iT = iT < 0 ? 0 : iT;
        iT = iT > maxT ? maxT : iT;
        
        oBox.style.marginTop = oBox.style.marginLeft = 0;
        oBox.style.left = iL + "px";
        oBox.style.top = iT + "px";    
        aPos.push({x:iL, y:iT})
        
        status();
        
        return false;
    };

    //鼠标释放, 结束拖拽
    document.onmouseup = window.onblur = oH2.onlosecapture = function (){
        bDrag = false;                
        oH2.releaseCapture && oH2.releaseCapture();
        status();
    };
    
    //回放拖动轨迹
    oA.onclick = function (){
        if (aPos.length == 1) return;
        var timer = setInterval(function ()    {
            var oPos = aPos.pop();
            oPos ? (oBox.style.left = oPos.x + "px", oBox.style.top = oPos.y + "px", status()) : clearInterval(timer)
        }, 30);        
        this.focus = false;//去除链接虚线        
        return false;
    };
    
    //阻止冒泡
    oA.onmousedown = function (event){
        (event || window.event).cancelBubble = true
    };
    
    //监听状态函数
    function status (){
        aSpan[0].innerHTML = bDrag;
        aSpan[1].innerHTML = oBox.offsetTop;
        aSpan[2].innerHTML = oBox.offsetLeft;
    }
    
    //初始调用
    status();
};
</script>
</head>
<body>
<div id="box">
    <h2><a href="javascript:;">点击回放拖动轨迹</a></h2>
    <p><strong>Drag:</strong><span></span></p>
    <p><strong>offsetTop:</strong><span></span></p>
    <p><strong>offsetLeft:</strong><span></span></p>
</div>
</body>
</html>

View Code

javascript代码

//鼠标按下, 激活拖拽
    oH2.onmousedown = function (event){        
        var event = event || window.event;
        bDrag = true;
        disX = event.clientX - oBox.offsetLeft;
        disY = event.clientY - oBox.offsetTop;        
        aPos.push({x:oBox.offsetLeft, y:oBox.offsetTop});        
        this.setCapture && this.setCapture();        
        return false;
    };
    
    //拖拽开始
    document.onmousemove = function (event){
        if (!bDrag) return;
        var event = event || window.event;
        var iL = event.clientX - disX;
        var iT = event.clientY - disY;
        var maxL = document.documentElement.clientWidth - oBox.offsetWidth;
        var maxT = document.documentElement.clientHeight - oBox.offsetHeight;
        
        iL = iL < 0 ? 0 : iL;    
        iL = iL > maxL ? maxL : iL; 
        
        iT = iT < 0 ? 0 : iT;
        iT = iT > maxT ? maxT : iT;
        
        oBox.style.marginTop = oBox.style.marginLeft = 0;
        oBox.style.left = iL + "px";
        oBox.style.top = iT + "px";    
        aPos.push({x:iL, y:iT})        
        status();        
        return false;
    };

    //鼠标释放, 结束拖拽
    document.onmouseup = window.onblur = oH2.onlosecapture = function (){
        bDrag = false;                
        oH2.releaseCapture && oH2.releaseCapture();
        status();
    };
    //阻止冒泡
    oA.onmousedown = function (event){
        (event || window.event).cancelBubble = true
    };
    
    //监听状态函数
    function status (){
        aSpan[0].innerHTML = bDrag;
        aSpan[1].innerHTML = oBox.offsetTop;
        aSpan[2].innerHTML = oBox.offsetLeft;
    }

参考:

  • https://developer.mozilla.org/en-US/docs/Web/API/Element.setCapture
  • http://msdn.microsoft.com/en-us/library/ie/ms536742%28v=vs.85%29.aspx
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/135014.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月5日 下午10:16
下一篇 2022年6月5日 下午10:16


相关推荐

  • 编程干货|10个超好用的python代码,短小且精悍,建议收藏!

    编程干货|10个超好用的python代码,短小且精悍,建议收藏!当今 python 编程语言的潮流已经成为不可阻挡的趋势 python 以其较高的可读性和简洁性备受程序员的喜爱 而 python 编程中的一些小的技巧 运用的恰当 会让你的程序事半功倍 以下的 10 个小的程序段 看似非常的简单 但是却非常的有技巧性 并且对个人的编程能力是一个很好的检验 大家应该在日常的编程中多多使用 多多练习 1 代码执行消耗时间利用 time 函数 在核心程序开始前记住当前时间点 然后在程序结束后计算当前时间点和核心程序开始前的时间差 可以帮助我们计算程序执行所消耗的时间 imp

    2025年11月3日
    3
  • pycharm远程连接服务器开发

    pycharm远程连接服务器开发使用 pycharm 远程连接服务器 Django 项目点击 next 只要账号密码输入成功 且服务器开启状态下 会连接成功以下方法也可以直接设置成功

    2026年3月16日
    1
  • coding平台_codeserver github

    coding平台_codeserver github这年头,IDE虽然也便宜了,不过,免费,还如此强大的就不多了。Codio,官方号称世界上最强大的基于浏览器的强大免费WebIDE,口号很响亮,当然,我也没用过,希望有朋友用了的,也来冒个泡。因为自己也是才接触这个,看了些国外用户的反馈,感觉还不错。这里就主要给寻找IDE的朋友们推荐这个东西。Codio是一个功能强大的云计算和基于浏览器的IDE(webide),从原型到部署,涵盖了完整的web…

    2022年8月31日
    7
  • MySql 三大日志:binlog、redo log 和 undo log

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 Keeper导读:日志是mysql数据库的重要组成部分,记录着数据库运行期间各种状态信息。mysql日志主要包括错误…

    2021年6月24日
    92
  • 解决Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile

    解决Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compilemvncleanpackage-Dmaven.test.skip=true今天项目用maven命令打包时候抛出错误:Failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile(default-compile)onprojectspringbootdemo:Fata…

    2022年8月22日
    11
  • java调用HTTP接口(Get请求和Post请求)

    java调用HTTP接口(Get请求和Post请求)前提:一个Http接口:http://172.83.38.209:7001/NSRTRegistration/test/add.do?id=8888888&name=99999999id和name是传入的参数浏览器访问接口:java代码调用Http接口代码如下(代码中注释分为两部分:处理get请求和post请求):packagecom.inspur.OKHTTP…

    2022年5月24日
    818

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号