鼠标按下时是否可以更改可拖动元素的 z-Index?

发布于 2024-10-07 05:32:26 字数 321 浏览 3 评论 0原文

我发现JQuery UI Draggablestack选项非常有用,但它只有在拖动元素时才起作用。那么,我可以更改stack值< /code> 当我**单击元素**使用 mousedown 事件 时可拖动的元素?

非常感谢~~!!

可拖动插件更改可拖动元素堆栈[z-index]顺序的方式是拖动和移动元素。如果您只单击可拖动元素,它不会更改堆栈顺序。而且我希望它在我时改变单击。

I found that the stack option of JQuery UI draggable was very useful , but it only work when I dragging the elements .So, can I change the stack value of elements which draggable just when I **click the elements** or with the mousedown event ??

Thank you very much~~!!

The way the draggable plugin to change draggable elements stack[z-index] order is dragging and moving the element.If you only click the draggable element it doesn't change the stack order.And I want it to change when I click.

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

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

发布评论

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

评论(4

旧夏天 2024-10-14 05:32:26

这不适合你吗?

$(element).mousedown(function(){
    ...
})

Is this not working for you?

$(element).mousedown(function(){
    ...
})
蓝天 2024-10-14 05:32:26

注意请参阅我对您的问题的评论,我认为我两次在下面写下我都误解了它。


如果您希望确保拖动过程中拖动的项目始终高于其他所有项目,则可以在与您的可拖动对象匹配的选择器中设置 z-index 代码>ui-draggable-dragging。 jQuery UI 将该类添加到拖动操作期间被拖动的元素中。

例如,假设您正在拖动名为 foo 的 div 内的跨度。 CSS 将为:

#foo span.ui-draggable-dragging {
    z-index: 1000 !important;   /* Sadly, you may need the !important */
}

Live example

由于该类仅在拖动期间添加,因此 z -index值仅在拖动过程中生效。请参阅文档中的“概述”。

编辑: ...通过使用 zIndex 选项 (实例)。

Note See my comment on your question, I think both times I was writing the below I misunderstood it.


If you're looking to ensure that the item you're dragging is always above everything else during the drag, you can set the z-index in a selector matching your draggable with the class ui-draggable-dragging. jQuery UI adds that class to the element being dragged during the drag operation.

So for instance, let's say you're dragging spans that are inside a div called foo. The CSS would be:

#foo span.ui-draggable-dragging {
    z-index: 1000 !important;   /* Sadly, you may need the !important */
}

Live example

Since the class is only added during the drag, that z-index value will only take effect during the drag. See the "overview" in the docs.

Edit: ...by using the zIndex option (live example).

哆兒滾 2024-10-14 05:32:26

您可以将以下代码放在可拖动插件(jsfiddle)之后:

(function ($) {
    var _create =  $.ui.draggable.prototype._create;

    $.ui.draggable.prototype._create = function () {
        var self = this;

        self.element.mousedown(function (e) {
            self._mouseStart(e);
            self._trigger('start', e);
            self._clear();
        });

        _create.call(self);
    };
})(jQuery);

或者,您可能想要将其制作为到一个单独的选项中,如下所示(jsfiddle):

(function ($) {
    $.ui.plugin.add('draggable', 'increaseZindexOnmousedown', {
        create: function () {
            this.mousedown(function (e) {
                var inst = $(this).data('draggable');
                inst._mouseStart(e);
                inst._trigger('start', e);
                inst._clear();
            });
        }
    });
})(jQuery);

You might place the following code after the draggable plugin (jsfiddle):

(function ($) {
    var _create =  $.ui.draggable.prototype._create;

    $.ui.draggable.prototype._create = function () {
        var self = this;

        self.element.mousedown(function (e) {
            self._mouseStart(e);
            self._trigger('start', e);
            self._clear();
        });

        _create.call(self);
    };
})(jQuery);

Or, you might want to make it into a separate option as follows (jsfiddle):

(function ($) {
    $.ui.plugin.add('draggable', 'increaseZindexOnmousedown', {
        create: function () {
            this.mousedown(function (e) {
                var inst = $(this).data('draggable');
                inst._mouseStart(e);
                inst._trigger('start', e);
                inst._clear();
            });
        }
    });
})(jQuery);
白龙吟 2024-10-14 05:32:26

由于我在搜索时发现了这个问题,所以我想我会发布我找到的答案。单击不会更改 z-index 的原因是拖动尚未开始。要强制在所有单击时启动拖动,请将距离选项设置为 0:

http:// /docs.jquery.com/UI/Draggable#option-distance

该行为与默认行为几乎相同,只是现在即使没有鼠标移动,z 顺序也会发生变化。

Since I found this question when I was searching, I figured I would post the answer I found. The reason a click does not change the z-index is that the drag hasn't initiated yet. To force the drag to be initiated on all clicks, set the distance option to 0:

http://docs.jquery.com/UI/Draggable#option-distance

The behavior is nearly the same as the default, except now z-order changes even with no mouse motion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文