如何通过拖放来重新排序dom中的canvas元素?
我想允许对类似 Photoshop 的图层进行拖放重新排序。
假设我在页面中有 3 个画布元素,id 为 a、b 和 c:
<canvas id="a"></canvas>
<canvas id="b"></canvas>
<canvas id="c"></canvas>
c 是“顶层”层,因为它是最后定义的,并且它们绝对位于彼此之上;如何将元素重新排列为以下顺序:c、a、b,其中 c 现在位于其他层的后面?
<canvas id="c"></canvas>
<canvas id="a"></canvas>
<canvas id="b"></canvas>
我想通过检测列表的拖放排序来触发排序,如下所示:
<ul id="layers">
<li>Layer C</li>
<li>Layer B</li>
<li>Layer A</li>
</ul>
我可能可以使用 jQuery ui sort 来允许对列表进行重新排序。使画布 DOM 顺序反映列表顺序的最简单方法是什么? (镜像有点相反,因为顶部有 C 的列表映射到底部有 C 的画布。
I want to allow drag-and-drop re-ordering of photoshop-like layers.
Assuming I have 3 canvas elements in the page with ids a, b and c:
<canvas id="a"></canvas>
<canvas id="b"></canvas>
<canvas id="c"></canvas>
c is the layer "on-top" because it is defined last, and they are positioned absolutely on top of each other; how can I rearrange the elements into the order: c, a, b, where c is now behind the other layers?
<canvas id="c"></canvas>
<canvas id="a"></canvas>
<canvas id="b"></canvas>
I want to trigger the sorting by detecting drag-and-drop sorting of a list like this:
<ul id="layers">
<li>Layer C</li>
<li>Layer B</li>
<li>Layer A</li>
</ul>
I can probably use jQuery ui sort to allow re-ordering of the list. What is the easiest way to make the canvases DOM order mirror the list order? (the mirroring is somewhat in reverse because the list with C on-top maps to canvas having C on bottom.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调整 z 分数绝对是正确的选择。我使用了一种样式来处理我关心的所有元素。这是我最近的一个项目的一些片段。
像这样的:
类很简单:
调整焦点被称为可拖动的开始事件:
Adjusting the z-score is definitely the way to go. I used a style to handle this for all the elements I care about. Here are some snippets from a recent project of mine.
Something like this:
The class is simply:
Adjust focus is called as the start event for draggable:
我相信你必须改变画布的 z 顺序,而不仅仅是改变它们的顺序,还是我错了?无论如何,我建议这样:
在你的 HTML 中:
编辑:哦,如果你想颠倒它们的放置顺序,只需使用
prependTo
而不是appendTo
。I believe you have to change the z-order of the canvases instead of just altering their order, or am I mistaken? Anyway, I'd suggest something like this:
And in your HTML:
Edit: oh, and if you want to invert the order they are placed, just use
prependTo
instead ofappendTo
.