如何使用 JQuery 在两个元素之间画一条线并刷新该线?
我正在使用 JQuery-UI Draggables 和 Droppables 将元素克隆到 div 上。 使用 JQuery 在页面上的元素之间绘制线条的最佳方法是什么?
刷新页面上的行的最佳方法是什么?我将在页面上有多行,并且只想更新特定行而不是刷新每一行。
I am using JQuery-UI draggables and droppables to clone elements onto a div.
What is the best way to draw a line between elements on a page using JQuery.
What is the best way to refresh lines on the page? I will have multiple lines on the page and only want to update a particular line rather than refresh every line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在有这个工作。
根据我的经验,不要使用 jquery.svg,这可能是在不学习另一个插件的情况下解决它的压力,但我发现它比它的价值更麻烦,并导致兼容性问题。
可以使用 HTML5 canvas 和 excanvas 兼容性脚本 和 一个很棒的 html5 演练,但是由于 HTML5 画布的工作原理,如果需要绘制线条,则需要销毁并重新绘制画布上的所有线条被删除或者其位置需要更新。
我在其之间绘制线条的元素位于表示关系的父元素内。子元素代表开始和结束,因此我可以通过使用例如
$('.relationshipClass')
获取父元素的集合并询问集合元素的子元素以获取父元素的集合来重新绘制所有这些关系线上的点。要使用此代码,您必须想出一种方法来轻松获取可重绘的线点。
标记:
漂亮又简单,一个 html
来保存在之间绘制的任何元素(最有可能是 JQuery UI 可拖动元素),以及一个
将在相同位置
CSS:
不要使用 CSS 控制
元素的宽度,请参阅 关于画布拉伸的问题。将
放置在与
相同的位置并位于其后面(使用 z 索引),这将显示 < 后面的线条code>
阻止与
子级的任何拖放交互。
Javascript方法:
创建实用方法:示例代码位于 JQuery 插件 内,其中包含:
添加新线条或调整线条位置时,会破坏现有线条并画出所有的线条。
您可以将下面的代码放入常规函数中或作为插件保留。
Javascript代码:
注意匿名后未进行测试。
您现在可以从事件处理程序(例如 JQuery UI 的拖动事件)调用这些函数画线。
I now have this working.
In my experience, don't use jquery.svg, it may have been the pressure to solve it without learning my way around another plugin, but I found it more hassle than it was worth and caused compatibilty issues.
It's possible to solve using the HTML5 canvas and excanvas compatibility script, and a great html5 walkthrough, BUT because of how the HTML5 canvas works, it requires that all the linse on the canvas are destroyed and redrawn if a line needs to be removed or its position needs to be updated.
The elements that I draw lines between are inside a parent element that represents a relationship. The child elements represent a start and end, so I can redraw all of these relationships by getting a collection of the parents using e.g.
$('.relationshipClass')
and interrogating the set's elements' children to get the points of the line.To use this code you will have to come up with an approach to easily get the line points available to redraw.
Markup:
Nice and simple, a html
<div>
to hold any elements that are drawn between (most probably JQuery UI draggables), and a<canvas>
that will be in the same positionCSS:
Don't control the
<canvas>
element's width with CSS, see Question on Canvas streching. Position the<canvas>
in the same position as the<div>
and behind it (with the z-index), this will show the lines up behind the<div>
and prevent the<canvas>
from blocking any drag and drop interactions with the<div>
's children.Javascript approach:
Create utility methods: the example code is inside a JQuery plugin that contains:
When you add a new line or adjust the position of a line, you destroy the existing lines and draw all of the lines.
You can put the code below into conventional functions or leave as a plugin.
Javascript code:
N.B. not tested after anonymisation.
You can now call these functions from event handlers (e.g. JQuery UI's drag event) to draw lines.
制作一个高度为 1px 的 hr 或 div 设置背景,并在需要时使用 jquery 为其宽度设置动画。
Make a hr or div with 1px height set background, and animate its width using jquery when needed.