خرید بک لینک
Hi people, I have a small issue regarding implementing Boundaries for a draggable object.

I caot get the coding correct and i keep ruing into errors. It really is a simple fix as well and i am sure that a more experienced coder would know such a simple attribute.

If any body would be so kind as to help guide me in the right direction i would be very great-full as i caot find any accurate information on the web as of yet.

In short, how do i code this correctly. Thank you



Code:

var selected = null,
    x_pos = 0, y_pos = 0,
    x_elem = 0, y_elem = 0;


// Will be called when user starts dragging an element
function _drag_init(elem) {
    // Store the object of the element which needs to be moved
    selected = elem;
    x_elem = x_pos - selected.offsetLeft;
    y_elem = y_pos - selected.offsetTop;




 // --- This is my addition to the code , and it is not respponding well at all //
       


                if (x_elem > 200 || x_elem < 10) {
                return;
               
        };
                if (y_elem >200 || y_elem < 10) {
                        return;
                       
                        };
}
 // ------------------------------------------------------------



function _move_elem(e) {
    x_pos = document.all ? window.event.clientX : e.pageX;
    y_pos = document.all ? window.event.clientY : e.pageY;
    if (selected !== null) {
        selected.style.left = (x_pos - x_elem) + 'px';
        selected.style.top = (y_pos - y_elem) + 'px';
   
       
       
}

function _destroy() {
    selected = null;
}

// Bind the functions...
document.getElementById('draggable-element').onmousedown = function () {
    _drag_init(this);
    return false;
};

document.onmousemove = _move_elem;
document.onmouseup = _destroy;

برچسب: نویسنده: آیلین رضوانی تاريخ: شنبه 20 خرداد 1396 ساعت: 4:51

صفحه بندی