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; | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you ever find a solution to this?