您的位置:首页 > Web前端 > JavaScript

拖拽、移动元素的JS原生函数

2017-04-18 11:14 197 查看
oDiv.onmousedown = function (ev) {

                var ev = ev || window.event;

                disX = ev.clientX - oDiv.offsetLeft;

                disY = ev.clientY - oDiv.offsetTop;

                document.onmousemove = function (ev) {

                    var ev = ev || window.event;

                    var iL = ev.clientX - disX;

                    var iT = ev.clientY - disY;

                    var maxL = document.documentElement.clientWidth - oDiv.offsetWidth;

                    var maxT = document.documentElement.clientHeight - oDiv.offsetHeight;

                    iL <= 0 && (iL = 0);

                    iL >= maxL && (iL = maxL);

                    iT <= 0 && (iT = 0);

                    iT >= maxT && (iT = maxT);

                    oDiv.style.left = iL + "px";

                    oDiv.style.top = iT + "px";

                    return false;

                };

                document.onmouseup = function () {

                    document.onmousemove = null;

                    document.onmouseup = null;

                    this.releaseCapture && this.releaseCapture();

                };

                this.setCapture && this.setCapture();

                return false;

            };
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JS拖拽 JS移动
相关文章推荐