Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

My code allows for the user to move a div around a container div by clicking and dragging the top, much like a window on a desktop. It does this by waiting for mousedown then mousemove, getting current curser location and moving the div via css margin-left and margin-top relative to the curser location. Once the user lifts the mouse, the current css margin-left and margin-top are retrieved and a new mousemove event is used to override the previous one and set the div to its current position each time the mouse moves. I'm wondering if there is a way of doing this so that:

1) there does not need to be a new mousemove event simply for moving the mouse after releasing it.

2) code allows the mouse to drag the .frame-container from any place within .move-frame-container without having to snap to a specific starting position (i.e: e.pageX - 70, e.pageY -10).

$(document).ready(function() {
$('.move-frame-container').mousedown(function() {    
        $(this).mousemove(function(e){  
            var horz_next = e.pageX - 70;
            var vert_next = e.pageY -10;
            $(".frame-container").css({"margin-left":horz_next});
            $(".frame-container").css({"margin-top":vert_next});
    });
}); 
$('.move-frame-container').mouseup(function() {
        var horz_after = $(".frame-container").css("margin-left");
        var vert_after = $(".frame-container").css("margin-top");
        $('.move-frame-container').mousemove(function(){
            $(".frame-container").css({"margin-left":horz_after});
            $(".frame-container").css({"margin-top":vert_after});
        });
    });
});  

The whole thing

http://jsfiddle.net/f8Heg/21/

share|improve this question
2  
Have you checked this out? jqueryui.com/draggable – Jonny Sooter Feb 19 at 15:15
what kind of performance issues would be resolved or presented if I chose to use this UI instead of writing my own simplified drag functionality? I am still awaiting some advice on how to simplify the above code but your link does spark some curiosity. – Drew Paul Feb 20 at 15:40
In my opinion, why re-invent the wheel? It's jQuery. Which means it will have better cross-browser support, the functionality is all there and ready to go, and it has great documentation that can help you customize it however you like. Plus it'll be a lot easier to maintain and update if needed. – Jonny Sooter Feb 20 at 16:54
That UI is hyper customizable and easy to use. I've recently used it myself to make this: kent.k12.wa.us/page/3712 – Jonny Sooter Feb 20 at 17:00
this isn't necessarily a solution, but it solved my problem. You have my gratitude. – Drew Paul Feb 21 at 1:32

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.