javascript - Limit a Draggable Element JS - Stack Overflow

i new in JS, i have this script that do exactly what i want, but i need to limit the draggable area, ri

i new in JS, i have this script that do exactly what i want, but i need to limit the draggable area, right now you can move the div all the way through the body and more i just want to be draggable inside an limited area.

This is the fiddle: /

This is the JS

    var Draggable = function (id) {
    var el = document.getElementById(id),
        isDragReady = false,
        dragoffset = {
            x: 0,
            y: 0
        };
    this.init = function () {
        //only for this demo
        this.initPosition();
        this.events();
    };
    //only for this demo
    this.initPosition = function () {
        el.style.position = "absolute";
        el.style.top = "0";
        el.style.left = "36%";
    };
    //events for the element
    this.events = function () {
        var self = this;
        _on(el, 'mousedown', function (e) {
            isDragReady = true;
            //corssbrowser mouse pointer values
            e.pageX = e.pageX || e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            e.pageY = e.pageY || e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
            dragoffset.x = e.pageX - el.offsetLeft;
            dragoffset.y = e.pageY - el.offsetTop;
        });
        _on(document, 'mouseup', function () {
            isDragReady = false;
        });
        _on(document, 'mousemove', function (e) {
            if (isDragReady) {
                e.pageX = e.pageX || e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
                e.pageY = e.pageY || e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
                el.style.top = (e.pageY - dragoffset.y) + "px";
                el.style.left = (e.pageX - dragoffset.x) + "px";
            }
        });
    };
    //cross browser event Helper function
    var _on = function (el, event, fn) {
        document.attachEvent ? el.attachEvent('on' + event, fn) : el.addEventListener(event, fn, !0);
    };
    this.init();
}

new Draggable('drag');

I Hope you can help me with this.

i new in JS, i have this script that do exactly what i want, but i need to limit the draggable area, right now you can move the div all the way through the body and more i just want to be draggable inside an limited area.

This is the fiddle: https://jsfiddle/Rafas/nbbg08mg/

This is the JS

    var Draggable = function (id) {
    var el = document.getElementById(id),
        isDragReady = false,
        dragoffset = {
            x: 0,
            y: 0
        };
    this.init = function () {
        //only for this demo
        this.initPosition();
        this.events();
    };
    //only for this demo
    this.initPosition = function () {
        el.style.position = "absolute";
        el.style.top = "0";
        el.style.left = "36%";
    };
    //events for the element
    this.events = function () {
        var self = this;
        _on(el, 'mousedown', function (e) {
            isDragReady = true;
            //corssbrowser mouse pointer values
            e.pageX = e.pageX || e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            e.pageY = e.pageY || e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
            dragoffset.x = e.pageX - el.offsetLeft;
            dragoffset.y = e.pageY - el.offsetTop;
        });
        _on(document, 'mouseup', function () {
            isDragReady = false;
        });
        _on(document, 'mousemove', function (e) {
            if (isDragReady) {
                e.pageX = e.pageX || e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
                e.pageY = e.pageY || e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
                el.style.top = (e.pageY - dragoffset.y) + "px";
                el.style.left = (e.pageX - dragoffset.x) + "px";
            }
        });
    };
    //cross browser event Helper function
    var _on = function (el, event, fn) {
        document.attachEvent ? el.attachEvent('on' + event, fn) : el.addEventListener(event, fn, !0);
    };
    this.init();
}

new Draggable('drag');

I Hope you can help me with this.

Share Improve this question edited Aug 11, 2015 at 16:22 Rafael Prato asked Aug 11, 2015 at 15:35 Rafael PratoRafael Prato 1722 silver badges11 bronze badges 2
  • I don't see the limited area you speak of – Maria Ines Parnisari Commented Aug 11, 2015 at 15:48
  • Hi could be anything, i just need to know how i put it on the JS. – Rafael Prato Commented Aug 11, 2015 at 16:22
Add a ment  | 

1 Answer 1

Reset to default 4

You can add check before moving element:

    _on(document, 'mousemove', function (e) {
        if (isDragReady) {
            e.pageX = e.pageX || e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            e.pageY = e.pageY || e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
            // left/right constraint
            if (e.pageX - dragoffset.x < 0) {
                offsetX = 0;
            } else if (e.pageX - dragoffset.x + 102 > document.body.clientWidth) {
                offsetX = document.body.clientWidth - 102;
            } else {
                offsetX = e.pageX - dragoffset.x;
            }

            // top/bottom constraint   
            if (e.pageY - dragoffset.y < 0) {
                offsetY = 0;
            } else if (e.pageY - dragoffset.y + 102 > document.body.clientHeight) {
                offsetY = document.body.clientHeight - 102;
            } else {
                offsetY = e.pageY - dragoffset.y;
            }   

            el.style.top = offsetY + "px";
            el.style.left = offsetX + "px";
        }

Demo fiddle

102 is width of Your element, You can get it with jQuery, or save it on _on(el, 'mousedown', function (e) {}) event.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744904034a4600144.html

相关推荐

  • javascript - Limit a Draggable Element JS - Stack Overflow

    i new in JS, i have this script that do exactly what i want, but i need to limit the draggable area, ri

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信