0

Here's my problem:
A system that i'm building requires a custom version of jQuery's draggable function.
The problem is that i can't get the element itself to be contained, i've tried several methods.

FIDDLE: http://jsfiddle.net/f8w8v/18/

Method 1

        var mouseStartPosition = {
                x: event.pageX,
                y: event.pageY,
            },
            elementPosition = {
                left: parseInt($sender.css('left')),
                top: parseInt($sender.css('top')),
            };

        $(window).on('mousemove',function(esub){
            var subEvent = eventData(esub).event,
                subSender = eventData(esub).sender,
                $subSender = $(subSender),
                mx = (subEvent.pageX - mouseStartPosition.x) || 0,
                my = (subEvent.pageY - mouseStartPosition.y) || 0,
                position = {
                    left : (parseInt($subSender.offset().left) <= 0) ? 0 : (elementPosition.left + mx),
                    top : (parseInt($subSender.offset().top) <= 0) ? 0 : (elementPosition.top + my),
                };
            $sender.css(position);
        });

Using this method causes the element to snap to 0px but it snaps off on mousemove even if offset is below 0.


Method 2

if(position.left <= 0){ 
    $sender.css('left','0px');
    return false; 
} else if( position.top <= 0) { 
    $sender.css('top','0px');
    return false;
} else {
    $sender.css(position); 
}

This methods reacts the same way but it's even worse, using return false causes the element to ignore mouse move in the opposite axis. The rest of the code is equal to method 2.

Summary: I can't find out how to contain the movin

Jeroen Vorsselman
  • 803
  • 1
  • 9
  • 19
  • 1
    You're saying jQuery isn't allowed in your question title, but you're using a jQuery plugin in your question, so is jQuery allowed or not? – Bojangles Aug 26 '14 at 17:57
  • I noticed that the title was a bit off, i've updated it. I meant that i need an alternative version of draggable than the jQuery version. – Jeroen Vorsselman Aug 26 '14 at 18:22

0 Answers0