Created
November 3, 2011 17:03
-
-
Save timbenniks/1337057 to your computer and use it in GitHub Desktop.
I am creating a very simple horizontal draggable script. How can I add containment? Any tips?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() | |
{ | |
$('.drag').bind('mousedown', function(mouseDownEvent) | |
{ | |
mouseDownEvent.preventDefault(); | |
var dragger = $(this), | |
mouseDownPageX = mouseDownEvent.pageX, | |
draggerOnMouseDownLeftPos = dragger.position().left, | |
leftPos, | |
topPos; | |
$(document).bind('mousemove mouseup', function(event) | |
{ | |
switch(event.type) | |
{ | |
case 'mousemove': | |
leftPos = (draggerOnMouseDownLeftPos + event.pageX) - mouseDownPageX, | |
dragger.css({left: leftPos}); | |
break; | |
case 'mouseup': | |
$(this).unbind('mousemove mouseup'); | |
break; | |
} | |
}); | |
}); | |
}); |
Chris, I had that. Once you find the draggable hits the bounding box, you set the left position to the max x value. After that its not draggable anymore as it does not listen to the x value anymore...
Did you ever find a solution to this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bounding container with set dimensions and conditionals to check the x value on mouse move?
Not so nice though...