【问题标题】:Drag div using jquery使用jquery拖动div
【发布时间】:2014-10-16 08:40:00
【问题描述】:

我正在尝试在不使用 jQuery UI 的情况下使 div 可拖动。

HTML

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

脚本

$.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

但我的代码不起作用。怎么了?如何让它发挥作用?

【问题讨论】:

标签: jquery html css


【解决方案1】:
(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);

用法:

$('div').drags();

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

【讨论】:

    【解决方案2】:

    使用 jqueryUI 总是更好。没有它你可以拖动,但它不会提供只有 jqueryUI 才能提供的平滑度。

    示例

    Link 1

    Link 2

    所以我建议使用任何拖动插件来使您的功能更加流畅。

    使用的功能

    $(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');
    });
    });
    

    【讨论】:

    • 我尝试使用链接 2 中的解决方案,但是当我尝试使用 position() 而不是 offset() 时,它不起作用。怎么了? jsfiddle.net/Jge9z/2054
    猜你喜欢
    • 1970-01-01
    • 2010-10-12
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2012-07-01
    • 2017-03-20
    • 1970-01-01
    相关资源
    最近更新 更多