使用实时事件进行 jQuery 拖放

发布于 2024-08-13 02:04:11 字数 204 浏览 6 评论 0原文

我有一个应用程序,其中有一个经常更改的长列表,并且我需要该列表中的项目可拖动。

我一直在使用 jQuery UI 可拖动插件,但添加到 400 多个列表项时速度很慢,并且每次添加新列表项时都必须重新添加。

有谁知道类似于 jQuery UI 可拖动插件的插件,它使用 jQuery 1.3 的 .live() 事件?这将解决这两个问题。

I have an application with a long list that changes frequently, and I need the items of that list to be draggable.

I've been using the jQuery UI draggable plugin, but it is slow to add to 400+ list items, and has to be re-added every time new list items are added.

Does anyone know of a plugin similar to the jQuery UI draggable plugin that uses jQuery 1.3's .live() events? This would solve both problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

情何以堪。 2024-08-20 02:04:11

Wojtek 的解决方案非常适合我。我最终对其进行了一些更改以使其扩展 jQuery...

(function ($) {
   $.fn.liveDraggable = function (opts) {
      this.live("mouseover", function() {
         if (!$(this).data("init")) {
            $(this).data("init", true).draggable(opts);
         }
      });
      return this;
   };
}(jQuery));

现在不要像这样调用它:

$(selector).draggable({opts});

...只需使用:

$(selector).liveDraggable({opts})

Wojtek's solution worked perfectly for me. I wound up changing it a tad bit to make it extend jQuery...

(function ($) {
   $.fn.liveDraggable = function (opts) {
      this.live("mouseover", function() {
         if (!$(this).data("init")) {
            $(this).data("init", true).draggable(opts);
         }
      });
      return this;
   };
}(jQuery));

Now instead of calling it like:

$(selector).draggable({opts});

...just use:

$(selector).liveDraggable({opts})
从来不烧饼 2024-08-20 02:04:11

这是一个非常适合我的代码示例

$('.gadgets-column').live('mouseover',function(){
    $(this).draggable();
});

This is a sample of code that perfectly worked for me

$('.gadgets-column').live('mouseover',function(){
    $(this).draggable();
});
卷耳 2024-08-20 02:04:11

你可以像这样制作包装函数:(

function liveDraggable(selector, options){
  jQuery(selector).live("mouseover",function(){
    if (!jQuery(this).data("init")) {
      jQuery(this).data("init", true);
      jQuery(this).draggable(options);
    }
  });
}

我使用 jQuery 的原型 - 这就是我放置 jQuery() 而不是 $() 的原因)

现在使用 liveDraggable(selector, {opts}) 而不是 $(selector).draggable({opts}) })

You could make wrapper function like this:

function liveDraggable(selector, options){
  jQuery(selector).live("mouseover",function(){
    if (!jQuery(this).data("init")) {
      jQuery(this).data("init", true);
      jQuery(this).draggable(options);
    }
  });
}

(I use prototype with jQuery - that's why i placed jQuery() instead of $())

And now instead of $(selector).draggable({opts}) use liveDraggable(selector, {opts})

╰つ倒转 2024-08-20 02:04:11

Stldoug 的代码对我有用,但无需在每个鼠标悬停事件上继续检查元素的 .data("init") 。另外,最好使用“mousemove”,因为当 .live 函数启动时,如果鼠标已经位于元素上方,则“mouseover”并不总是会被触发。

(function ($) {
    $.fn.liveDraggable = function (opts) {
        this.live("mousemove", function() {
            $(this).draggable(opts);
        });
    };
}(jQuery));

以下是使用它的方法:

$('.thing:not(.ui-draggable)').liveDraggable();

技巧是添加“:not” (.ui-draggable)”到您的选择器。由于当元素变得可拖动时,jQuery 会自动将“ui-draggable”类添加到元素中,因此 .live 函数将不再以它为目标。换句话说,它只触发一次,不像其他解决方案,当你移动东西时会一遍又一遍地触发。

理想情况下,您可以只取消绑定“mousemove”,但不幸的是,这不适用于 .live。

Stldoug's code worked for me, but there's no need to keep checking the element's .data("init") on every mouseover event. Also, it's better to use "mousemove", as "mouseover" doesn't always get triggered if your mouse is already over the element when the .live function kicks in.

(function ($) {
    $.fn.liveDraggable = function (opts) {
        this.live("mousemove", function() {
            $(this).draggable(opts);
        });
    };
}(jQuery));

Here's how you use it:

$('.thing:not(.ui-draggable)').liveDraggable();

The trick is to add ":not(.ui-draggable)" to your selector. Since jQuery will automatically add the "ui-draggable" class to your element when it becomes draggable, the .live function will no longer target it. In other words, it only triggers once, unlike the other solution which triggers over and over as you move stuff around.

Ideally, you could just .unbind the "mousemove", but that doesn't work with .live, unfortunately.

回忆追雨的时光 2024-08-20 02:04:11

结合 @john 和 @jasimmk 的最佳答案:

使用 .live:

$('li:not(.ui-draggable)').live('mouseover',function(){
    $(this).draggable(); // Only called once per li
});

.live 已弃用,最好使用 .on:

$('ul').on('mouseover', 'li:not(.ui-draggable)', function(){
    $(this).draggable();  // Only called once per li
});

正如 @john 所解释的,.ui-draggable 会自动添加到可拖动方法中,因此通过使用选择器排除该类,您可以确保draggable()每个元素只会被调用一次。并且使用 .on 将缩小选择器的范围,从而提高性能。

Combining the best answers from @john and @jasimmk:

Using .live:

$('li:not(.ui-draggable)').live('mouseover',function(){
    $(this).draggable(); // Only called once per li
});

.live is deprecated though, better to use .on:

$('ul').on('mouseover', 'li:not(.ui-draggable)', function(){
    $(this).draggable();  // Only called once per li
});

As @john explained, .ui-draggable is automatically added to draggable methods, so by excluding that class with the selector, you ensure that draggable() will only be called once on each element. And using .on will reduce the scope of the selector, improving performance.

醉殇 2024-08-20 02:04:11

示例:

土耳其语:

<div id="diyalogKutusu">
    <div id="diyalog-baslik">..baslik..</div>
    <div id="icerik">..icerik..</div>
</div>

$(document).on("mouseover", "#diyalogKutusu", function() {
    $(this).draggable({ handle: '#diyalog-baslik' });
});

英语:

<div id="dialogBox">
    <div id="dialogBox-title">..title..</div>
    <div id="content">..content..</div>
</div>

$(document).on("mouseover", "#dialogBox", function() {
    $(this).draggable({ handle: '#dialogBox-title' });
});

注意:您可以使用 on() 代替 live()delegateon() 比其他方法具有更好的性能

An example:

Turkish:

<div id="diyalogKutusu">
    <div id="diyalog-baslik">..baslik..</div>
    <div id="icerik">..icerik..</div>
</div>

$(document).on("mouseover", "#diyalogKutusu", function() {
    $(this).draggable({ handle: '#diyalog-baslik' });
});

English:

<div id="dialogBox">
    <div id="dialogBox-title">..title..</div>
    <div id="content">..content..</div>
</div>

$(document).on("mouseover", "#dialogBox", function() {
    $(this).draggable({ handle: '#dialogBox-title' });
});

Note: You can use on() instead of live() or delegate. The on() has good performance than others

忆依然 2024-08-20 02:04:11
$("html divs to drag").appendTo("#layoutDiv").draggable(options);

JSFiddle

$("html divs to drag").appendTo("#layoutDiv").draggable(options);

JSFiddle

网白 2024-08-20 02:04:11

一个老问题。但 Threedubmedia 具有拖放插件,支持实时(从 v 1.7 开始,简称为“on”)支持。
http://tridubmedia.com/code/event/drop
没有太多使用它,所以我无法解释它的性能等,但看起来很合理。

An old question. But threedubmedia has drag and drop plugin with live (as of v 1.7 known as simply "on") support.
http://threedubmedia.com/code/event/drop
Haven't used it to much so I can't account for it performance, etc. but looks reasonable.

羅雙樹 2024-08-20 02:04:11

另一种选择是将鼠标悬停处理程序与可移动类混合使用,如下所示:

$('.outer-container').on('mouseover', '.my-draggable.drag-unbound', function(e) {
  $(this).draggable().removeClass('drag-unbound');
});

它相当简单,并且解决了其他答案在鼠标悬停时一遍又一遍地重新绑定所带来的一些问题。

Another option is to mix the mouseover handler with a removable class, like so:

$('.outer-container').on('mouseover', '.my-draggable.drag-unbound', function(e) {
  $(this).draggable().removeClass('drag-unbound');
});

It's fairly straightforward and resolves some of the issues that other answers have with re-binding over and over as you mouseover.

灯角 2024-08-20 02:04:11

不使用 live 的更新版本,因为它已被弃用:

function liveDraggable(selector, options) {
    $(document).on('mouseover', selector, function () {
        if (!$(this).data("init")) {
            $(this).data("init", true);
            $(this).draggable(options);
        }
    });
}

An updated version that does not use live as it is deprecated:

function liveDraggable(selector, options) {
    $(document).on('mouseover', selector, function () {
        if (!$(this).data("init")) {
            $(this).data("init", true);
            $(this).draggable(options);
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文