js在一个div里面移动其子div

        var ChildDiv = $("#cid");
            var width = 0;  //鼠标点击子div的地方和子div的左边边距距离
            var height = 0;  //鼠标点击子div的地方和子div的上边边距距离
            ChildDiv.onmousedown = function (ev) //鼠标按下DIV
            {
                var ChildDivEvent = ev || event;   //捕获点击的对象
                width = ChildDivEvent.clientX - ChildDiv.offsetLeft;
                height = ChildDivEvent.clientY - ChildDiv.offsetTop;
                document.onmousemove = function (ev)   //鼠标移动
                {
                    var ChildDivEvent = ev || event;
                    var x = ChildDivEvent.clientX - width;
                    var y = ChildDivEvent.clientY - height;
                    if (x < 0) //当DIV的Left小于0,也就是移出左边
                    {
                        x = 0; //就把DIV的Left设置为0,就不能移出左边
                    } else if (x > $("#pid").width() - ChildDiv.offsetWidth) //DIV不能移出父DIV的右边
                    {
                        x = $("#pid").width() - ChildDiv.offsetWidth;
                    }
                    if (y < 0) //上边
                    {
                        y = 0;
                    } else if (y > $("#pid").height() - ChildDiv.offsetHeight) //下边
                    {
                        y = $("#pid").height() - ChildDiv.offsetHeight;
                    }
                    ChildDiv.style.left = x + 'px'; //DIV的Left设置为新鼠标X坐标减去width的值
                    ChildDiv.style.top = y + 'px'; //DIV的Top设置为新鼠标Y坐标减去height的值
                };
                document.onmouseup = function () //鼠标松开时
                {
                    document.onmousemove = null; //把鼠标移动清除
                    document.onmouseup = null; //把鼠标松开清除
                };
                return false;
            };

 

转载于:https://www.cnblogs.com/zhylioooo/p/8257786.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值