Jquery:在单击的行旁边显示img

发布于 2024-10-13 07:23:12 字数 894 浏览 8 评论 0原文

我正在尝试为我的网站创建书签功能,因此当有人单击“设置书签”然后单击一行文本时,它将在该行的左侧放置一个书签图像。 (然后我会将coor保存到cookie中,但我可以在没有帮助的情况下做到这一点)

我想到的一种方法是当人们单击段落中的一行文本时,让它抓取该位置的Y坐标该人点击的数字,然后将该数字向下舍入为最接近的可被 20 整除的数字。每个段落的行高为 20px,因此,如果您向下舍入 Y 坐标,那么您将获得段落内该单行的顶部位置他们点击了。

所以我认为它会是这样的:当有人点击段落中的一行文本时,它会得到该段落的索引,所以如果它是页面下方的第4个P,索引将为3,然后它会得到用户单击位置的 Y 坐标,然后将该数字向下舍入到可被 20 整除的最接近的数字,然后将图像放置在该段落的左侧,图像的顶部位置作为舍入的 Y 坐标。

谁能帮我解决这个问题吗?正如你所看到的,我有点迷失了:

$('p').click(function(e) {
    var myIndex = $(this).index()
    var myIndexTop = myIndex.top()
    var myIndexLeft = myIndex.left()

    var offset = $(this).offset();
    var y = e.pageY - this.offsetTop;

    $('.bookMarkImg')
        .left(myIndexLeft)
        .top('round down to nearest num thats divisible by 20)
OR?     
    $('.bookMarkImg')
        .css({'left': myIndexLeft, 'top' 'round down to nearest num thats divisible by 20'})
})

I'm trying to create a bookmarking feature for my site, so when someone clicks on "set bookmark" then they click on a line of text, it will place a bookmark image to the left of that line. (i'll then save the coor to a cookie, but i can do that without help)

One way I thought about doing it was when the person clicked on a line of text in a paragraph, have it grab the Y coordinate of the place the person clicked, then have that number rounded down to the nearest number divisible by 20. The line height of each paragraphs is 20px, so if you rounded the Y coordinates down, then you would get the top position for that single line inside the paragraph they clicked.

So I think it would go like this: when someone clicks on a line of text in a paragraph, it will get the index of that paragraph, so if it's the 4th P down the page, the index will be 3, then it will get the Y coordinates of where the user clicked, then round that number down to the nearest number divisible by 20, then place the image to the left of that paragraph with the TOP position of the image being the rounded Y coordinate.

Can anyone help me out with this? I'm kind of lost as you can see:

$('p').click(function(e) {
    var myIndex = $(this).index()
    var myIndexTop = myIndex.top()
    var myIndexLeft = myIndex.left()

    var offset = $(this).offset();
    var y = e.pageY - this.offsetTop;

    $('.bookMarkImg')
        .left(myIndexLeft)
        .top('round down to nearest num thats divisible by 20)
OR?     
    $('.bookMarkImg')
        .css({'left': myIndexLeft, 'top' 'round down to nearest num thats divisible by 20'})
})

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

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

发布评论

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

评论(1

晚雾 2024-10-20 07:23:12

要向下舍入到最接近的 20:

y = Math.floor(y / 20) * 20;

此外,.top().left() 不存在:您需要:

  • $(this) .offset().top$(this).offset().left 用于获取位置
  • .css({left: ..., top: ... }) 用于设置它们

另一件事是,如果您只希望其中一个书签图像浮动,具体取决于您单击的位置,那么您可能需要放置 < /code> 位于 级别(最后),并且绝对定位,因此可以在整个文档中自由移动。然后,不必费心从 y 坐标中减去段落的偏移量。

示例代码

$('p').click(function (e) {
    var offset = $(this).offset();
    var top = Math.floor(e.pageY / 20) * 20;

    $('.bookMarkImg').css({
        left: offset.left,
        top: top
    }).show();
});

查看工作演示http://jsfiddle. net/upgBa/

注意:不过,您可能必须考虑段落和正文上的任何填充/边距/边框,这可能会导致计算(只需减去其中的“顶部”部分)。

To round down to the nearest 20:

y = Math.floor(y / 20) * 20;

Also, .top() and .left() don't exist: you'll want:

  • $(this).offset().top and $(this).offset().left for getting the positions
  • .css({left: ..., top: ...}) for setting them

Another thing is that if you only want one of these bookmark images to be floating around, depending on where you click, then you'll probably want to put the <img> at the <body> level (at the end), and positioned absolutely, so it is free to move around the entire document. Then, don't bother subtracting the offset of the paragraph from the y coordinate.

Sample code:

$('p').click(function (e) {
    var offset = $(this).offset();
    var top = Math.floor(e.pageY / 20) * 20;

    $('.bookMarkImg').css({
        left: offset.left,
        top: top
    }).show();
});

See a working demo: http://jsfiddle.net/upgBa/

Note: You may have to consider any padding/margin/border you have on the paragraphs and the body though, which could throw off the calculations (just subtract the "top" portions of these).

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