jQuery - 选择覆盖在图像上的不可见文本,ala GMail PDF 查看器
我将不可见的文本叠加在图像之上。是否有 jQuery 插件(或类似插件)允许用户选择图像上的区域(也选择覆盖的文本)并能够复制内容。
现在,我已将每个字符放入其自己的 标记中。问题是当用户选择时,它有时会选择所有覆盖的文本(除非用户使用鼠标非常精确),有时图像本身也会被选中,等等。
类似于 GMail 的 PDF 查看器的解决方案会很好。建议?
I'm overlaying invisible text on top of an image. Is there a jQuery plugin (or similar) that will allow users to select an area on the image (which also selects the overlaid text) and be able to copy the content.
Right now, I have placed each character in its own <span />
tag. Problem is when user selects, it sometimes select all the overlaid text (unless user is extremely precise with his/her mouse), sometimes the image itself becomes selected, etc.
A solution similar to GMail's PDF viewer would be nice. Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Google 似乎从 pdf 中知道文件中各种
x,y
文本偏移量的位置。当您选择一堆行时,它会在“文本”所在的图像上放置一组绝对定位的“选择”div(它们具有highlight-pane
类)。当您选择文本时,它会使用您所选择的内容填充#selection-content
文本区域,并选择其中的文本(尝试使用window.getSelection().anchorNode< /code> 在 Chrome 中,例如)。除了这些选择叠加之外,还有一个图像
.page-image
。我敢打赌他们实际上使用窗口来捕获他们关心的所有鼠标手势(我假设mousedown
和mouseup
)。 (这是一个 pdf 文档示例< /a>)如果您对元素进行绝对定位,则可以检测
mousedown
、mousemove
和mouseup
,找出跨度元素 mouse结束(或最接近),并用这两个元素之间的所有内容填充文本区域。如果您只想使用单词粒度,我怀疑有人会抱怨(用跨度包围每个单词,而不是每个字母)。编辑:昨晚我有点好奇,编写了一个非常天真的版本。它只执行
mousedown
和mouseup
,并且在 IE 中不起作用(我不想调试它:)在 jsfiddle 上查看。
您可能想要添加的功能:
mousemove
上的动态更新Google seems to know from a pdf where the various
x,y
text offsets are in the file. When you select a bunch of lines, it places a set of absolutely positioned "selection" divs over the image where the "text" is (they have classhighlight-pane
). When you select text, it fills in a#selection-content
textarea with the contents of what you have selected, and selects the text in it (try usingwindow.getSelection().anchorNode
in Chrome, e.g.). Besides those selection overlays, there is just an image.page-image
. I bet they actually use window to capture all the mouse gestures they care about (I assumemousedown
andmouseup
). (Here's an example pdf document)If you're absolute-positioning the elements, you could detect
mousedown
,mousemove
andmouseup
, figure out the span elements mouse is over (or nearest to), and fill in a textarea with the contents of all content between those two elements. If you want to just use word-granularity, I doubt anyone would complain (surround each word with a span, rather than each letter).Edit: I got kinda curious last night and coded up a really naive version. It only does
mousedown
andmouseup
, and it doesn't work in IE (I don't feel like debugging it :)Check it out on jsfiddle.
Features you might want to add:
mousemove
这是一个使用我对上一个问题的回答的简单示例: http:// /www.jsfiddle.net/yijian/83W7X/2/embedded/result
它将 jQuery UI 的 Selectable 与 Tim Down 优秀的 Rangy 库混合在一起,创建与您要求的类似的东西。我认为。你问的不是很清楚。
该代码保留当前选定的 li 元素的数组。代码的第二部分添加了相关的事件处理程序和选项。每次选择或取消选择元素时都会调用
drawSelection
函数。该函数首先按 DOM 中的位置对所有元素进行排序,然后继续从第一个选定的li
到最后一个选定的内容进行选择。代码,例如 theazureshadow 的< /a> 只是概念验证,因为我正在抽象实际上应该是为相当重的 Rangy 库选择 li 的简单任务。它的性能也不是很好,需要进行一些重构。
Here's a simple example using my answer to your previous question: http://www.jsfiddle.net/yijiang/83W7X/2/embedded/result
It mixes jQuery UI's Selectable with Tim Down's excellent Rangy library to create something similar to what you asked for. I think. What you asked for wasn't exactly clear.
The code keeps an array of currently selected
li
elements. The second part of the code adds in the relevant event handlers and options. ThedrawSelection
function is called every time an element is selected or deselected. The function first sorts all elements by their position in the DOM, then proceeds to draw a selection from the first selectedli
to the last.The code, like theazureshadow's, is proof-of-concept only, since I'm abstracting what really should be the simple task of selecting the
li
s to the rather heavy Rangy library. It also does not perform very well and could do with some refactoring.