1

I'm trying to make a div draggable without using jQuery UI.

HTML

<div class="wrapper">
  <div class="toddler"></div>
</div>

Script

$.fn.slider = function () {
$(this).on("mousedown", function () {
    $dragging = true;
});

$(this).on("mouseup", function () {
    $dragging = null;
});

$(this).on("mousemove", function () {
    if ($dragging) {
        $(this).offset({
            left: $(this).pageX
        });
    }
});
};

$(".toddler").slider();

my fiddle

but my code doesn't work. What's wrong? How to make it work?

Suganth G
  • 5,136
  • 3
  • 25
  • 44
truslivii.lev
  • 701
  • 1
  • 8
  • 21

2 Answers2

2
(function($) {
$.fn.drags = function(opt) {

    opt = $.extend({handle:"",cursor:"move"}, opt);

    if(opt.handle === "") {
        var $el = this;
    } else {
        var $el = this.find(opt.handle);
    }

    return $el.css('cursor', opt.cursor).on("mousedown", function(e) {
        if(opt.handle === "") {
            var $drag = $(this).addClass('draggable');
        } else {
            var $drag = $(this).addClass('active-handle').parent().addClass('draggable');
        }
        var z_idx = $drag.css('z-index'),
            drg_h = $drag.outerHeight(),
            drg_w = $drag.outerWidth(),
            pos_y = $drag.offset().top + drg_h - e.pageY,
            pos_x = $drag.offset().left + drg_w - e.pageX;
        $drag.css('z-index', 1000).parents().on("mousemove", function(e) {
            $('.draggable').offset({
                top:e.pageY + pos_y - drg_h,
                left:e.pageX + pos_x - drg_w
            }).on("mouseup", function() {
                $(this).removeClass('draggable').css('z-index', z_idx);
            });
        });
        e.preventDefault(); // disable selection
    }).on("mouseup", function() {
        if(opt.handle === "") {
            $(this).removeClass('draggable');
        } else {
            $(this).removeClass('active-handle').parent().removeClass('draggable');
        }
    });

}
})(jQuery);

Usage:

$('div').drags();

http://css-tricks.com/snippets/jquery/draggable-without-jquery-ui/

ReLeaf
  • 1,368
  • 1
  • 10
  • 17
1

It is always better to use jqueryUI. You can drag without it but it will not give the smoothness which can only be provided by jqueryUI.

Examples

Link 1

Link 2

So i suggest to use any drag plugin to give smoothness to your function.

Function used

$(function() {
$('body').on('mousedown', 'div', function() {
    $(this).addClass('draggable').parents().on('mousemove', function(e) {
        $('.draggable').offset({
            top: e.pageY - $('.draggable').outerHeight() / 2,
            left: e.pageX - $('.draggable').outerWidth() / 2
        }).on('mouseup', function() {
            $(this).removeClass('draggable');
        });
    });
    e.preventDefault();
}).on('mouseup', function() {
    $('.draggable').removeClass('draggable');
});
});
Vivek Dragon
  • 2,218
  • 4
  • 27
  • 48
  • I tried to use solution from Link 2, but when I tried to use `position()` instead of `offset()` it doesn't work. What's wrong? http://jsfiddle.net/Jge9z/2054/ – truslivii.lev Aug 22 '14 at 14:54