拖拽原理-问题-解决办法

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
</style>
<script>
window.onload = function() {

    /*
    1.拖拽的时候,如果有文字被选中,会产生问题
        原因:当鼠标按下的时候,如果页面中有文字被选中,那么会触发浏览器默认拖拽文字的效果
            解决:
                标准:阻止默认行为
                非标准ie:全局捕获
    */

    var oDiv = document.getElementById('div1');

    oDiv.onmousedown = function(ev) {
        var ev = ev || event;

        var disX = ev.clientX - this.offsetLeft;
        var disY = ev.clientY - this.offsetTop;

        if ( oDiv.setCapture ) {
            oDiv.setCapture();
        }

        document.onmousemove = function(ev) {
            var ev = ev || event;

            oDiv.style.left = ev.clientX - disX + 'px';
            oDiv.style.top = ev.clientY - disY + 'px';
        }

        document.onmouseup = function() {
            document.onmousemove = document.onmouseup = null;
            //释放全局捕获 releaseCapture();
            if ( oDiv.releaseCapture ) {
                oDiv.releaseCapture();
            }
        }

        return false;

    }

}
</script>
</head>

<body>
    jafldsfjdsjfkl
    <div id="div1"></div>
</body>
</html>

例二:限制范围的拖拽和磁性吸附
限制范围原理:当被拖拽的元素到达限制范围的边沿的时候,让目标元素的Left或Top的值等于限制范围的值。
磁性吸附原理:当目标元素快要到达吸附的边沿的时候,让目标元素的Left或Top值等于吸附的边沿值。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
</style>
<script>
window.onload = function() {

    var oDiv = document.getElementById('div1');

    drag(oImg);

    drag(oDiv);

    function drag(obj) {

        obj.onmousedown = function(ev) {
            var ev = ev || event;

            var disX = ev.clientX - this.offsetLeft;
            var disY = ev.clientY - this.offsetTop;

            if ( obj.setCapture ) {
                obj.setCapture();
            }

            document.onmousemove = function(ev) {
                var ev = ev || event;

                var L = ev.clientX - disX;
                var T = ev.clientY - disY;


                if ( L < 100 ) {
                    L = 0;
                } else if ( L > document.documentElement.clientWidth - obj.offsetWidth ) {
                    L = document.documentElement.clientWidth - obj.offsetWidth;
                }

                if ( T < 0 ) {
                    T = 0;
                } else if ( T > document.documentElement.clientHeight - obj.offsetHeight ) {
                    T = document.documentElement.clientHeight - obj.offsetHeight;
                }

                obj.style.left = L + 'px';
                obj.style.top = T + 'px';

            }

            document.onmouseup = function() {
                document.onmousemove = document.onmouseup = null;
                if ( obj.releaseCapture ) {
                    obj.releaseCapture();
                }
            }

            return false;

        }

    }

}
</script>
</head>

<body>
    <div id="div1"></div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值