根据像素偏移在 DIV 中查找特定文本

发布于 2024-08-11 03:25:20 字数 416 浏览 4 评论 0 原文

我想将图像标签插入到文本块中,以便图像在 DIV 中向下 100 像素。

    <div>
    A lot of text in a text block and a <img tag> somewhere
that is floated left or right and positioned inside the text block
    </div>

因此,上面的标签被放入要浮动的文本块中,使其位于 DIV 中向下 100px 的位置。现在,这不能静态地完成,它必须在 javascript 中完成,因为 div 中向下 100px 可能由不同的文本表示,具体取决于字体渲染问题等。

那么,有没有办法找到 DIV 下方 100px 的文本块中的“单词”呢?也许是 jQuery?

I want to insert an image tag into a block of text so that the image is, say, 100 pixels down in the DIV.

    <div>
    A lot of text in a text block and a <img tag> somewhere
that is floated left or right and positioned inside the text block
    </div>

So, the tag above is put in the text block to be floated so that it is positioned at 100px DOWN in the DIV. Now, this can't be done statically, it has to be done in javascript since 100px down in the div may be represented by different text depending on font rendering issues and such.

So, is there a way to find what "word" in a text block that is 100px down in the DIV? jQuery perhaps?

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

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

发布评论

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

评论(2

北陌 2024-08-18 03:25:20

简短的回答是

这归结为 DOM 没有为您提供获取元素像素位置的方法。您可以对某些浏览器进行一些修改,但祝您好运,让解决方案发挥作用。

如果无法获取包含 div 的位置,则无法获取其中文本的位置。

抛开,如果您要找到一种方法来获取 div 的像素高度,我将遵循类似于以下的过程。

  1. 从 div 内部获取文本并将其存储在本地变量中。
  2. 在文本中每个单词的循环内:
  3. 在单词之前插入 div 标签。
  4. 获取 div 标签的像素位置。
  5. 用它来确定与像素位置向下 100 最接近的单词。
  6. 获得最接近的位置后,创建一个文档片段来构建 div 的新内部,
  7. 用文档片段替换 div 的内容。

The short answer is no

Basically it comes down to the DOM doesn't give a way for you to get an elements pixel position. There are sort of hacks you can do for some browsers, but good luck getting a solution working.

If you cannot get the position of the containing div, you are not going to get the position for the text within.

Leaving aside the no, if you were to find a way to get the div's pixel height, I would follow a procedure similar to the following.

  1. Get the text from inside the div and store it in a local var.
    1. Inside a loop for each word in the text:
    2. Insert a div tag into just before the word.
    3. Get the pixel position of the div tag.
    4. Use this to determine the closest word to pixel pos 100 down.
  2. Once you have the closest position, create a document fragment to build up your new inner for the div
  3. Replace the div's content with the document fragment.
魔法唧唧 2024-08-18 03:25:20

聚会迟到了,很抱歉可能出现题外话。就我而言,文本存储为

中的

,并且我必须在 的垂直中心找到该元素(因此文本也是如此) div (用于根据其内容长度将 div 动态“拆分”为 2 列布局)。所以我做了这个:

function getElAtTheVerticalCenterOf(el) {
  //get el's rect and positions
  const bodyRect = document.body.getBoundingClientRect(),
        elRect = el.getBoundingClientRect(),
        scroll = elRect.y - bodyRect.y,
        marginLeft = elRect.x;

  //store current window scroll
  const orgX = window.scrollX, orgY = window.scrollY;

  //scroll to the vertical center of el to find the target
  const elHeight = el.clientHeight;
  window.scrollTo(0, scroll + elHeight/2);
  const target = document.elementFromPoint(marginLeft, 0);

  //scroll back to original window scroll
  window.scrollTo(orgX, orgY);

  return target;
}

主要思想是:

  1. 获取滚动量。
  2. window 滚动到所需位置(以便在那里使用 elementFromPoint())。
  3. 让目标元素
  4. 滚动回原始位置。

至于getBoundingClientRect()elementFromPoint(),请查找相应的文章。请注意,offsetTopint,对于此函数的工作来说不够准确。我简直不敢相信,但我必须坚持使用 getBoundingClientRect() (float) 而不是 offsetTop

Late to the party and sorry for the possible off-topic. In my case, texts are stored as <p>s in a <div> and I have to find the element (thus text too) in the vertical center of the div (for 'splitting' the div into 2-column layout dynamically according to its contents length). So I made this one:

function getElAtTheVerticalCenterOf(el) {
  //get el's rect and positions
  const bodyRect = document.body.getBoundingClientRect(),
        elRect = el.getBoundingClientRect(),
        scroll = elRect.y - bodyRect.y,
        marginLeft = elRect.x;

  //store current window scroll
  const orgX = window.scrollX, orgY = window.scrollY;

  //scroll to the vertical center of el to find the target
  const elHeight = el.clientHeight;
  window.scrollTo(0, scroll + elHeight/2);
  const target = document.elementFromPoint(marginLeft, 0);

  //scroll back to original window scroll
  window.scrollTo(orgX, orgY);

  return target;
}

Main idea is:

  1. get how much to scroll.
  2. scroll window to wanted position (in order to use elementFromPoint() there).
  3. get the target element
  4. scroll back to the original position.

As for getBoundingClientRect() or elementFromPoint(), please look for the corresponding articles. Note that offsetTop is int and was not enough accurate for this function to work. I can't believe it, but I had to stick to getBoundingClientRect() (float) instead of offsetTop.

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