开发者

flexible drag/drop with jquery (like this javascript)

I'm trying to understand how this drag/drop javascript example would be done in jquery. I'm guessing the jquery version 开发者_StackOverflow社区will be cleaner, more efficient, and easier to understand.


This is how i do it w/o using jQuery UI. Assumes you've styled .draggable to also be position: absolute:

var $draggable = null, startX, startY;

$('.draggable').live('mousedown', function(ev) {
    $draggable = $(this);
    var pos = $draggable.position();
    startX = ev.pageX - pos.left;
    startY = ev.pageY - pos.top;
    return false;
});

$(document).bind('mousemove', function(ev) {
    if ($draggable) {
        $draggable.css({ left: ev.pageX - startX, top: ev.pageY - startY});
        return false;
    }
});

$(document).bind('mouseup', function(ev) {
    if ($draggable) {
        $draggable = null;
        return false;
    }
});


Check out jQueryUI. They have a lot of cool stuff, including drag/drop, that's really easy to use.


Take a look on this from Jquery ui


Check out http://jqueryui.com/demos/draggable/ . you can view the source. You'll need to link to jquery and to jquery ui.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜