dojo - 鼠标(x,y) - 拖放 - 挫败感

发布于 2024-08-02 23:19:26 字数 10013 浏览 7 评论 0原文

我正在尝试使用 dojo.dnd 拖动对象,但希望头像位于与对象相同的位置(相对于鼠标),

即如果一个人单击对象的中间,则鼠标光标将位于头像的中间。

我得到了各种奇怪的结果。 如果我将函数连接到 body.onmousemove dnd 的放置部分会失败。

我怎样才能让它工作?



DnD 活动</code><br> <code><style type="text/css"></code><br> <code>.target</code><br> <代码>{</代码><br> <code>边框:1px 点状灰色;</code><br> <code>宽度:300px;</code><br> <code>高度:300px;</code><br> <code>内边距:5px;</code><br> <code>-moz-border-radius: 8pt 8pt;</code><br> <code>半径:8pt;</code><br> <代码>}</代码><br> <code>.source</code><br> <代码>{</代码><br> <code>边框:1px 虚线天蓝色;</code><br> <code>高度:200px;</code><br> <code>宽度:300px;</code><br> <code>-moz-border-radius: 8pt 8pt;</code><br> <code>半径:8pt;</code><br> <代码>}</代码><br> <code>.dojoDndItemOver</code><br> <代码>{</代码><br> <code>背景:#feb;</code><br> <code>边框:1px 点状灰色;</code><br> <代码>}</代码><br> <br><br> <code>.target .dojoDndItemAnchor</code><br> <代码>{</代码><br> <code>背景:#ededed;</code><br> <code>边框:1px纯灰色;</code><br> <代码>}</代码><br> <code>.dojoDndAvatarHeader {</code><br> <code>显示:无;</code><br> <代码>}</代码><br> <code></style></code><br> <code><script type="text/javascript" src="dojo/dojo.js" djconfig="parseOnLoad: true, isDebug:false"></script></code><br> <code><脚本类型=“text/javascript”></code><br> <code>dojo.require("dojo.dnd.Source");</code><br> <code>dojo.require("dojo.dnd.Container");</code><br> <code>dojo.require("dojo.dnd.Moveable");</code><br> <code>dojo.require("dojo.dnd.Manager");</code><br> <code>dojo.require("dojo.dnd.Avatar");</code><br> <代码></代码><br> <code>var mouse = { x: 0, y: 0 , 句柄:undefined};</code><br> <code>函数 mouseCoords(ev) {</code><br> <code>var px, py;</code><br> <code>ev = ev || window.event;</code><br> <code>if (ev.pageX || ev.pageY) {</code><br> <code>px = ev.pageX; py = ev.pageY;</code><br> <code>}其他{</code><br> <code>px = ev.clientX + dojo.body().scrollLeft - dojo.body().clientLeft;</code><br> <code>py = ev.clientY + dojo.body().scrollTop - dojo.body().clientTop;</code><br> <代码>}</代码><br> <br><br> <code>鼠标 = { x: px, y: py };</code><br> <code>// dojo.byId("msg").innerHTML = dojo.toJson(mouse);</code><br> <br><br> <代码>}</代码><br> <code>//dnd 当以下行被注释掉时有效。 (定位失败)</code><br> <code>var mchandle = dojo.connect(document, "onmousemove", 'mouseCoords');</code><br> <code>//dojo.query(".dojoDndItem").connect("onclick", 'mouseCoords');</code><br> <code>//dojo.dnd.Source.onMouseDown('mouseCoords')</code><br> <code></script></code><br> <code><脚本类型=“text/javascript”></code><br> <代码></代码><br> <br><br> <br><br> <代码></代码><br> <br><br> <code>var item_price;</code><br> <code>var 总计 = 0;</code><br> <code>函数AddItems(目标,节点){</code><br> <code>for (var i = 0; i <nodes.length; i++)</code><br> <code>{ Total += parseFloat((target.getItem(nodes[i].id)).data); }</code><br> <code>dojo.byId("cost").innerHTML = Total;</code><br> <代码>}</代码><br> <br><br> <code>函数 SubstractItems(目标,节点) {</code><br> <code>for (var i = 0; i <nodes.length; i++) {</code><br> <code>total -= parseInt((target.getItem(nodes[i].id)).data);</code><br> <代码>}</代码><br> <code>dojo.byId("cost").innerHTML = Total;</code><br> <代码>}</代码><br> <br><br> <code>函数 ShowPrice(目标,节点) {</code><br> <code>var sum = 0;</code><br> <code>for (var i = 0; i <nodes.length; i++) {</code><br> <code>dojo.dnd.manager().OFFSET_X = 0 - (mouse.x - dojo._abs(nodes[i]).x);</code><br> <code>dojo.dnd.manager().OFFSET_Y = 0 - (mouse.y - dojo._abs(nodes[i]).y);</code><br> <code>dojo.dnd.manager().updateAvatar();</code><br> <code>sum += parseInt((target.getItem(nodes[i].id)).data);</code><br> <代码>}</代码><br> <代码></代码><br> <code>dojo.byId("msg").innerHTML = "所选商品价格为 $" + sum;</code><br> <代码>}</代码><br> <br><br> <code>函数ClearMsg()</code><br> <code>{ dojo.byId("msg").innerHTML = ""; }</code><br> <br><br> <code>函数 init() {</code><br> <br><br> <br><br> <br><br> <code>dojo.subscribe("/dnd/drop", function(source, nodes, iscopy) {</code><br> <code>var t = dojo.dnd.manager().target;</code><br> <code>ClearMsg();</code><br> <code>if (t == 源) { return; }</code><br> if (t == cart) { AddItems(t, 节点); }</code><br> if (t == 架子) { SubstractItems(t, 节点); }</code><br> <代码></代码><br> <br><br> <code>});</code><br> <br><br> <code>dojo.subscribe("/dnd/start", function(source, nodes, iscopy) {</code><br> <code>ShowPrice(源,节点);</code><br> <code>});</code><br> <br><br> <code>dojo.subscribe("/dnd/cancel", function() {</code><br> <code>ClearMsg();</code><br> <code>});</code><br> <br><br> <代码>}</代码><br> <br><br> <br><br> <code>dojo.addOnLoad(init);</code><br> <br><br> <br><br> <br><br> <br><br> <br><br> <br><br> <br><br> <br><br> <code></script></code><br> <br><br> <代码></代码><br> <br><br> <code></head></code><br> <code><body style="font-size: 12px;"></code><br> <br><br> <code><表格></code><br> <code><tbody></code><br> <code><tr valign="top"></code><br> <code><td></code><br> <code>来源</code><br> <code><div dojotype="dojo.dnd.Source" jsid="shelf" class="source" id="source1"accept="red,blue"</code><br> <code>singular="false"></code><br> <code><img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="10" title="$10" /></code><br> <code><img src="RED.png" class="dojoDndItem" dndtype="red" dnddata="60" title="$60" /></code><br> <code><img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="13" title="$13" /></code><br> <code><img src="RED.png" class="dojoDndItem" dndtype="red" dnddata="15" title="$15" /></code><br> <code><img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="3" title="$3" /></code><br> <code><img src="RED.png" class="dojoDndItem" dndtype="red" dnddata="148" title="$148" /></code><br> <code><img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="1" title="$1" /></code><br> <code><img src="RED.png" class="dojoDndItem" dndtype="red" dnddata="10" title="$10" /></code><br> <code><img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="3" title="$3" /></code><br> <code></div></code><br> <code></td></code><br> <code><td></code><br> <code>目标</code><br> <code><div dojotype="dojo.dnd.Source" jsid="cart" class="target"accept="red,blue" id="target1"></code><br> <code></div></code><br> <code></td></code><br> <code><td></code><br> <code>总价格(美元):<span id="cost">0.00</span><br /></code><br> <code><b>消息:<span id="msg" style="color: blue"></span></b></code><br> <code><td></code><br> <code></tr></code><br> <code><tbody /></code><br> <code></table></code><br> <code></body></code><br> <code></html></code><br> <br><br> <br><br> <br> <br></p>

I'm attempting to drag an object using dojo.dnd but want the avatar to be in the same position as the object was (relative to the mouse)

i.e. if a person clicks in the middle of the object then the mouse cursor will be in the middle of the avatar.

I've had all sorts of strange results.
if i connect a function to body.onmousemove the drop part of the dnd fails.

How can i get this working?

<html>
<head>
<title>DnD Events</title>
<style type="text/css">
.target
{
border: 1px dotted gray;
width: 300px;
height: 300px;
padding: 5px;
-moz-border-radius: 8pt 8pt;
radius: 8pt;
}
.source
{
border: 1px dotted skyblue;
height: 200px;
width: 300px;
-moz-border-radius: 8pt 8pt;
radius: 8pt;
}
.dojoDndItemOver
{
background: #feb;
border: 1px dotted gray;
}

.target .dojoDndItemAnchor
{
background: #ededed;
border: 1px solid gray;
}
.dojoDndAvatarHeader {
display: none;
}
</style>
<script type="text/javascript" src="dojo/dojo.js" djconfig="parseOnLoad: true, isDebug:false"></script>
<script type="text/javascript">
dojo.require("dojo.dnd.Source");
dojo.require("dojo.dnd.Container");
dojo.require("dojo.dnd.Moveable");
dojo.require("dojo.dnd.Manager");
dojo.require("dojo.dnd.Avatar");

var mouse = { x: 0, y: 0 , handle:undefined};
function mouseCoords(ev) {
var px, py;
ev = ev || window.event;
if (ev.pageX || ev.pageY) {
px = ev.pageX; py = ev.pageY;
} else {
px = ev.clientX + dojo.body().scrollLeft - dojo.body().clientLeft;
py = ev.clientY + dojo.body().scrollTop - dojo.body().clientTop;
}

mouse = { x: px, y: py };
// dojo.byId("msg").innerHTML = dojo.toJson(mouse);

}
//dnd WORKS when following lines are commented out. (positioning fails)
var mchandle = dojo.connect(document, "onmousemove", 'mouseCoords');
//dojo.query(".dojoDndItem").connect("onclick", 'mouseCoords');
//dojo.dnd.Source.onMouseDown('mouseCoords')
</script>
<script type="text/javascript">

var item_price;
var total = 0;
function AddItems(target, nodes) {
for (var i = 0; i < nodes.length; i++)
{ total += parseFloat((target.getItem(nodes[i].id)).data); }
dojo.byId("cost").innerHTML = total;
}

function SubstractItems(target, nodes) {
for (var i = 0; i < nodes.length; i++) {
total -= parseInt((target.getItem(nodes[i].id)).data);
}
dojo.byId("cost").innerHTML = total;
}

function ShowPrice(target, nodes) {
var sum = 0;
for (var i = 0; i < nodes.length; i++) {
dojo.dnd.manager().OFFSET_X = 0 - (mouse.x - dojo._abs(nodes[i]).x);
dojo.dnd.manager().OFFSET_Y = 0 - (mouse.y - dojo._abs(nodes[i]).y);
dojo.dnd.manager().updateAvatar();
sum += parseInt((target.getItem(nodes[i].id)).data);
}

dojo.byId("msg").innerHTML = "Selected Item Price is $" + sum;
}

function ClearMsg()
{ dojo.byId("msg").innerHTML = ""; }

function init() {

dojo.subscribe("/dnd/drop", function(source, nodes, iscopy) {
var t = dojo.dnd.manager().target;
ClearMsg();
if (t == source) { return; }
if (t == cart) { AddItems(t, nodes); }
if (t == shelf) { SubstractItems(t, nodes); }

});

dojo.subscribe("/dnd/start", function(source, nodes, iscopy) {
ShowPrice(source, nodes);
});

dojo.subscribe("/dnd/cancel", function() {
ClearMsg();
});

}

dojo.addOnLoad(init);

</script>

</head>
<body style="font-size: 12px;">

<table>
<tbody>
<tr valign="top">
<td>
SOURCE
<div dojotype="dojo.dnd.Source" jsid="shelf" class="source" id="source1" accept="red,blue"
singular="false">
<img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="10" title="$10" />
<img src="RED.png" class="dojoDndItem" dndtype="red" dnddata="60" title="$60" />
<img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="13" title="$13" />
<img src="RED.png" class="dojoDndItem" dndtype="red" dnddata="15" title="$15" />
<img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="3" title="$3" />
<img src="RED.png" class="dojoDndItem" dndtype="red" dnddata="148" title="$148" />
<img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="1" title="$1" />
<img src="RED.png" class="dojoDndItem" dndtype="red" dnddata="10" title="$10" />
<img src="BLUE.png" class="dojoDndItem" dndtype="blue" dnddata="3" title="$3" />
</div>
</td>
<td>
TARGET
<div dojotype="dojo.dnd.Source" jsid="cart" class="target" accept="red,blue" id="target1">
</div>
</td>
<td>
Total Price (USD): <span id="cost">0.00</span><br />
<b>Message: <span id="msg" style="color: blue"></span></b>
<td>
</tr>
<tbody />
</table>
</body>
</html>

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

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

发布评论

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

评论(2

夏花。依旧 2024-08-09 23:19:26

从这个意义上说,Dojo 的 DND 是有限的。头像的位置偏移,因此移动事件不会被代表拖动的节点捕获。在这种情况下,典型的源/目标将不起作用。我创建了一个混合“mover/source”dnd 示例,可以帮助您完成您想要完成的任务:

http://svn.dojotoolkit.org/src/demos/trunk/beer/src/dnd.js

基本上,我们将“mousedown”连接到某个节点。当它被触发时,我们直接在页面中原始节点所在的位置创建该节点的克隆(请参阅 _dragStart 函数)。然后,我们注册临时的 mousemove 和 mouseup 事件监听器。 mousemove 是一个针对速度进行优化的紧凑函数。只需设置“头像”(克隆)相对于 e.pageX 和 e.pageY(标准化事件对象部分)的顶部/左侧位置。

当 mouseup 被触发时,我们会断开 mouseup 和 mousemove 事件(this._listeners)。在示例中,“overTarget”函数仅返回 true。您可以将此逻辑更改为您需要的任何内容,以确保当前 pageX/pageY 坐标位于您选择的边界框内(源/目标,无论您喜欢什么)。

在示例中,我让它动画回到“源”的原始 x/y,或者将其转换为 dojo.dnd.Moveable(它是克隆节点),从源中创建一种标记工厂。您可能只想使用它来将任何数据添加到您的购物车,并销毁头像。

希望这有帮助。

Dojo's DND is a limited in this sense. The avatar is positioned offset so the move events are not trapped by the node representing the drag. Typical source/targets will not work in this case. I have created a hybrid "mover/source" dnd example that may help you along with what you are trying to accomplish:

http://svn.dojotoolkit.org/src/demos/trunk/beer/src/dnd.js

Basically, we connect "mousedown" to some node. When that is fired, we create a clone of that node directly over where the the original node is in the page (see _dragStart function). Then, we register temporary mousemove and mouseup event listeners. mousemove is a tight function optimized for speed. simply set the top/left position of the "avatar" (the clone) relative to e.pageX and e.pageY (normalized event object parts).

when mouseup is fired, we disconnect both mouseup and mousemove events (this._listeners). In the sample the "overTarget" function simply returns true. You can change this logic to be whatever you need to ensure the current pageX/pageY coords are within a bounding box of your choice (a Source/Target, whatever, however you like).

In the example, I have it animating back to the original x/y of the "source", or turning it into a dojo.dnd.Moveable (it being the cloned node), creating a sort of markupfactory out of the source. You will likely want to just use this to add whatever data to your cart, and destroy the avatar.

Hope this helps.

不回头走下去 2024-08-09 23:19:26

暂时...(因为我不知道如何实现混合js)

我已经利用了取消事件并将鼠标坐标与页面上的每个源对象进行比较,以确定要删除哪一个进入。

页面中的 JS 现在:

var mchandle;
dojo.require("dojo.dnd.Source");
var lastSrc;
function init() {
    dojo.subscribe("/dnd/drop", function(source, nodes, iscopy) {
        dojo.byId("msg").innerHTML += " drop";
        dojo.disconnect(mchandle);
    });
    dojo.subscribe("/dnd/start", function(source, nodes, iscopy) {
        lastSrc = source;
        mchandle = dojo.connect(dojo.doc, "onmousemove", "mouseCoords");
        dojo.byId("msg").innerHTML = "start";
        var px = 0;
        var py = 0;
        for (var i = 0; i < nodes.length; i++) {
            var nPos = dojo._abs(nodes[i]);
            px = nPos.x > px ? nPos.x : px;
            py = nPos.y > py ? nPos.y : py;
        }
        dojo.dnd.manager().OFFSET_X = 0 - (source._lastX - px);
        dojo.dnd.manager().OFFSET_Y = 0 - (source._lastY - py);


    });
    dojo.subscribe("/dnd/cancel", function() {
        dojo.byId("msg").innerHTML += " cancel";
        dojo.query("[dojotype=\"dojo.dnd.Source\"]").forEach(function(node, index, array) {
            var elemXY = dojo.coords(node);
            if ( //in source box
            (elemXY.x <= document.mouse.x && document.mouse.x <= (elemXY.x + elemXY.w)) && (elemXY.y <= document.mouse.y && document.mouse.y <= (elemXY.y + elemXY.h))) {
                var s = new dojo.dnd.Source(node, null);
                s.insertNodes(true, lastSrc.getSelectedNodes(), null, null);
            }
        });
        dojo.disconnect(mchandle);
        lastSrc = null;
    });
}
dojo.addOnLoad(init);
function mouseCoords(ev) {
    var px, py;
    ev = ev || window.event;
    if (ev.pageX || ev.pageY) {
        px = ev.pageX;
        py = ev.pageY;
    } else {
        px = ev.clientX + dojo.body().scrollLeft - dojo.body().clientLeft;
        py = ev.clientY + dojo.body().scrollTop - dojo.body().clientTop;
    }
    document.mouse = {
        "x": px,
        "y": py
    };
}

仍然出现错误。
我拖动到目标(一切都很好。)
我从目标拖动,屏幕上粘着一个重复的头像。
萤火虫报告错误。

_5.getItem(_6[i].id) is undefined
} catch (e) {\r\n                                 dojo.js (line 203)

你能提供任何帮助吗?

For the time being...(since i didn;t know how to implement the hybrid js)

i've utilised the cancel event and compared the mouse coords to each source object on the page to determine which one it's was intended to be dropped into.

JS in page is now:

var mchandle;
dojo.require("dojo.dnd.Source");
var lastSrc;
function init() {
    dojo.subscribe("/dnd/drop", function(source, nodes, iscopy) {
        dojo.byId("msg").innerHTML += " drop";
        dojo.disconnect(mchandle);
    });
    dojo.subscribe("/dnd/start", function(source, nodes, iscopy) {
        lastSrc = source;
        mchandle = dojo.connect(dojo.doc, "onmousemove", "mouseCoords");
        dojo.byId("msg").innerHTML = "start";
        var px = 0;
        var py = 0;
        for (var i = 0; i < nodes.length; i++) {
            var nPos = dojo._abs(nodes[i]);
            px = nPos.x > px ? nPos.x : px;
            py = nPos.y > py ? nPos.y : py;
        }
        dojo.dnd.manager().OFFSET_X = 0 - (source._lastX - px);
        dojo.dnd.manager().OFFSET_Y = 0 - (source._lastY - py);


    });
    dojo.subscribe("/dnd/cancel", function() {
        dojo.byId("msg").innerHTML += " cancel";
        dojo.query("[dojotype=\"dojo.dnd.Source\"]").forEach(function(node, index, array) {
            var elemXY = dojo.coords(node);
            if ( //in source box
            (elemXY.x <= document.mouse.x && document.mouse.x <= (elemXY.x + elemXY.w)) && (elemXY.y <= document.mouse.y && document.mouse.y <= (elemXY.y + elemXY.h))) {
                var s = new dojo.dnd.Source(node, null);
                s.insertNodes(true, lastSrc.getSelectedNodes(), null, null);
            }
        });
        dojo.disconnect(mchandle);
        lastSrc = null;
    });
}
dojo.addOnLoad(init);
function mouseCoords(ev) {
    var px, py;
    ev = ev || window.event;
    if (ev.pageX || ev.pageY) {
        px = ev.pageX;
        py = ev.pageY;
    } else {
        px = ev.clientX + dojo.body().scrollLeft - dojo.body().clientLeft;
        py = ev.clientY + dojo.body().scrollTop - dojo.body().clientTop;
    }
    document.mouse = {
        "x": px,
        "y": py
    };
}

A Bug is still occurring.
I drag to the target (all is well.)
i drag from the target and a duplicate avatar is stuck on the screen.
firebug reports an error.

_5.getItem(_6[i].id) is undefined
} catch (e) {\r\n                                 dojo.js (line 203)

could you offer any help?

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