在 jQuery 可点击区域内创建死区

发布于 2024-09-11 14:01:41 字数 5410 浏览 4 评论 0原文

使用 jQuery jQuery-ui 和 ui 主题之一(我非常喜欢 亮度)包括

(请耐心等待,底部有一个问题,如果您愿意,可以跳到该问题,只是想展示我是如何到达现在的位置的)

(抱歉,如果我在代码中遗漏了任何错误或位,它确实有效,但为了便于阅读而切割了代码)

我从标准 可拖动 + 可排序jQuery-UI< 提供/a> 项目:

// HTML
<div id="leftColumn">
<ul id="pageElements">
<li class="ui-state-default ui-corner-all" id="html">Html</li>
<li class="ui-state-default ui-corner-all" id="paragraph">Paragraph</li>
<li class="ui-state-default ui-corner-all" id="image">Image</li>
<li class="ui-state-default ui-corner-all" id="faq">FAQ</li>
</ul>
</div>

<div id="rightColumn">
    <ul id="pageItems"></ul>
</div>

// JavaScript Code
$("#pageItems").sortable({
    revert: true,
    placeholder: 'ui-state-highlight',
    tolerance: 'pointer'
});
$("#pageElements li").draggable({
    connectToSortable: '#pageItems',
    helper: 'clone',
    revert: 'invalid'
 });

然后我开始考虑添加一个 dblclick 函数以允许项目从一个列表移动到另一个列表而不需要拖动会很有用,所以我添加了这个:

    $("#pageElements li").dblclick(function () {
        copythis(this);
    });

function copythis(elem) {
    var selected = $(elem).closest("li").clone();     
    $("#pageItems").append(selected).html();
}

然后我想好吧,现在我想要dblclick pageItem 并显示一个编辑对话框,

所以我添加了:

<div id="itemDialog"></div> 


$("#itemDialog").dialog({ autoOpen: false, modal:true });
$("#pageItems li").live("dblclick", function () {
    openDialog(this);
});

function openDialog(elem) {
    $("#itemDialog").dialog('open');
}

然后我想当我将鼠标悬停在元素上时需要一个按钮,以允许我删除,并且我也可能在那里有一个编辑路线:

var removeButton = '<span class="buttons"><span class="ui-edit ui-state-default ui-corner-all ui-icon ui-icon-pencil" title="edit">Edit</span><span class="ui-remove ui-state-default ui-corner-all ui-icon ui-icon-closethick" title="remove">Remove</span></span>';

$("#pageItems li").live("mouseenter", function () { $(this).append(addButton); }).live("mouseleave", function () { $(this).find(".buttons").remove(); });

    $(".ui-remove").live("click", function () { $(this).closest("li").remove(); });

    $(".ui-edit").live("click", function () {
        openDialog(this);
    });

所以现在它非常实用,我想向 pageElement 添加一个“添加”按钮,这样我就有了 3 个拖动、dblclick 和按钮单击选项,因此添加代码:

    var addButton = '<span class="buttons"><span class="ui-add ui-state-default ui-corner-all ui-icon ui-icon-triangle-1-e" title="Add">Add</span></span>';

使用 pageItem li 中的代码再次“实时悬停” :

$("#pageElements li").live("mouseenter", function () { $(this).append(addButton); }).live("mouseleave", function () { $(this).find(".buttons").remove(); });

$(".ui-add").live("click", function () {
    copythis(this);
});

我修改了 copythis 函数以允许包含按钮:

function copythis(elem) {
    var selected = $(elem).closest("li").clone();
    selected.find(".buttons").remove();
    $("#pageItems").append(selected).html();
}

这就是我现在所在的位置以及我当前正在解决的问题。

如果您双击 pageElement,则会复制该元素(如预期)。

如果您单击“添加”按钮,它也会复制一个新元素(如预期)。

因为 pageElement dblclick 事件附加到 pageElement li 标记,并且添加按钮单击事件位于嵌套在其中的 span 标记上,如果我快速单击添加按钮,有时我会触发 dblclick 事件,因此添加多个元素页面项目列表。

我想要做的是为 dblclick 事件创建一个死区,该死区围绕添加按钮项。

我对任何想法持开放态度。

干杯

卢克

With jQuery, jQuery-ui and one of the ui themes (I quite like lightness) included

(bear with me, there is a question at the bottom which you can skip to if you like, just wanted to show how I got to where I am)

(sorry if there are any mistakes or bits I have missed out in the code, it does work, but was cutting up the code for ease of reading)

I started out with the standard draggable + Sortable provide by the jQuery-UI project:

// HTML
<div id="leftColumn">
<ul id="pageElements">
<li class="ui-state-default ui-corner-all" id="html">Html</li>
<li class="ui-state-default ui-corner-all" id="paragraph">Paragraph</li>
<li class="ui-state-default ui-corner-all" id="image">Image</li>
<li class="ui-state-default ui-corner-all" id="faq">FAQ</li>
</ul>
</div>

<div id="rightColumn">
    <ul id="pageItems"></ul>
</div>

// JavaScript Code
$("#pageItems").sortable({
    revert: true,
    placeholder: 'ui-state-highlight',
    tolerance: 'pointer'
});
$("#pageElements li").draggable({
    connectToSortable: '#pageItems',
    helper: 'clone',
    revert: 'invalid'
 });

Then I started to think well it would be useful to add a dblclick function to allow items to be shifted from one list to another without needing to drag, so I added this:

    $("#pageElements li").dblclick(function () {
        copythis(this);
    });

function copythis(elem) {
    var selected = $(elem).closest("li").clone();     
    $("#pageItems").append(selected).html();
}

Then I thought ok, now I want to dblclick on the pageItem and show a dialog for edit

so I added:

<div id="itemDialog"></div> 


$("#itemDialog").dialog({ autoOpen: false, modal:true });
$("#pageItems li").live("dblclick", function () {
    openDialog(this);
});

function openDialog(elem) {
    $("#itemDialog").dialog('open');
}

Then I thought I want a button when I hover over an element, to allow me to delete, and I might as well have an edit route in there too:

var removeButton = '<span class="buttons"><span class="ui-edit ui-state-default ui-corner-all ui-icon ui-icon-pencil" title="edit">Edit</span><span class="ui-remove ui-state-default ui-corner-all ui-icon ui-icon-closethick" title="remove">Remove</span></span>';

$("#pageItems li").live("mouseenter", function () { $(this).append(addButton); }).live("mouseleave", function () { $(this).find(".buttons").remove(); });

    $(".ui-remove").live("click", function () { $(this).closest("li").remove(); });

    $(".ui-edit").live("click", function () {
        openDialog(this);
    });

So now it was quite functional, I wanted to then add an "add" button to the pageElement, so I would then have 3 options of drag, dblclick and a button click, so adding the code:

    var addButton = '<span class="buttons"><span class="ui-add ui-state-default ui-corner-all ui-icon ui-icon-triangle-1-e" title="Add">Add</span></span>';

used code from the pageItem li "live hover" again:

$("#pageElements li").live("mouseenter", function () { $(this).append(addButton); }).live("mouseleave", function () { $(this).find(".buttons").remove(); });

$(".ui-add").live("click", function () {
    copythis(this);
});

I amended the copythis function to allow for the inclusion of buttons:

function copythis(elem) {
    var selected = $(elem).closest("li").clone();
    selected.find(".buttons").remove();
    $("#pageItems").append(selected).html();
}

And this is where I am now and the problem I am currently working on.

If you dblclick the pageElement, that will copy the element (as expected).

If you click on the add button that will also copy a new element (as expected).

because the pageElement dblclick event is attached to the pageElement li tag and the add button click event is on a span tag nested inside that, if I click on the add button quickly sometimes I will get the dblclick event firing so adding more than one element to the pageItem list.

What I am looking to do is create a deadzone for the dblclick event which surrounds the add button item.

I am open to any ideas.

Cheers

Luke

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

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

发布评论

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

评论(2

谈情不如逗狗 2024-09-18 14:01:41

我知道这没有什么帮助,但我建议不要听 dblclick - 它 a) 在不同的客户端中工作方式不同 - 并且 jquery 不会掩盖所有这些,b) 你不希望你的用户双击,情况很复杂。

I know it's far from helpful, but I'd advise against listening to dblclick - it is a) works differently in different clients - and jquery doesn't mask all of it, b) you don't want your user to double click, it's complicated.

拥抱我好吗 2024-09-18 14:01:41

好的,所以我找到了一个答案,可能不是最好的,但它有效。

它涉及定义一个全局变量:

var clicked = 0;

然后在 ui-add 的实时事件上将值设置为 1,然后设置超时以延迟将值重置回 0:

    $(".ui-add").live("click", function () {
        clicked = 1;
        copythis(this);
        setTimeout(function () { clicked = 0;}, 100);
    });

那么然后在我的 pageElement dblclick 函数上:

    $("#pageElements li").dblclick(function () {
        if (clicked == 0) {
            copythis(this);
        }
    });

不理想,但是有用。

OK, so I found an answer, probably not the best, but it works.

It involves defining a global variable:

var clicked = 0;

Then on the live event of the ui-add set the value to 1 and then set a timeout to delay the reset of the value back to 0:

    $(".ui-add").live("click", function () {
        clicked = 1;
        copythis(this);
        setTimeout(function () { clicked = 0;}, 100);
    });

So then on my pageElement dblclick function:

    $("#pageElements li").dblclick(function () {
        if (clicked == 0) {
            copythis(this);
        }
    });

Not ideal, but it works.

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