设置 DIV 的高度以与该 DIV 内锚点的位置相对应

发布于 12-18 03:26 字数 780 浏览 3 评论 0原文

核心问题http://jsfiddle.net/pipip/P46Xg/
我有一个 div 容器,其中包含几段文本,
其中一个段落内有以下锚点
容器设置为 overflow:hidden

是否可以使用 javascript / JQuery 设置容器的高度,以便容器的底部正好停在锚点处或锚点下方?

增加深度和内容背景http://jsfiddle.net/pipip/yj9dB/
这将用于修改后的 JQuery Slider。使用 CMS 的人可以在“内容”字段中输入 [readmore] anywhere,该内容将通过 PHP 替换为上述锚点。这样,他们就可以轻松控制“阅读更多”中断在容器内出现的位置。在相关的示例中,我将高度硬编码为75px,尽管我想要的是高度取决于文本中锚点 name="stop" 的位置。

谢谢。如果这是一个糟糕的方法,我洗耳恭听!

Core issue : http://jsfiddle.net/pipip/P46Xg/
I have a div container with a few paragraphs of text,
and inside one of these paragraphs is the following anchor <a name="stop" />
The container is set to overflow:hidden

Is it possible with javascript / JQuery to set the height of the container so that the bottom of the container stops exactly at or below the anchor?

Added Depth & Background : http://jsfiddle.net/pipip/yj9dB/
This would be used for a modified JQuery Slider. Where someone using a CMS could type [readmore] anywhere into the Content field, which would be replaced by the above anchor via PHP. This way they would be easily able to control where the Read More break appears within the container. In the associated example I am hard-coding the height to 75px, although what I want is for the height to be dependent on the location of the anchor name="stop" in the text.

Thanks. If this is an awful way to go about it, I'm all ears!

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

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

发布评论

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

评论(2

绾颜2024-12-25 03:26:01

使用 jQuery,你可以这样做:

$('#container').height( $('a[name=stop]').position()['top'] );

和 CSS:

#container {
    position: relative;
}

我们需要这个,因为 .position() 给我们相对于元素的偏移父级的位置,我们可以通过将其position设置为<,使#container成为锚标记的偏移父级。代码>相对。

JSFiddle: http://jsfiddle.net/divad12/RYPm7/1/

另外,对于HTML5,您可能需要考虑设置锚标记的 id 属性而不是 nameHTML 锚点带有“name”或“id”?

With jQuery you could do it like this:

$('#container').height( $('a[name=stop]').position()['top'] );

and CSS:

#container {
    position: relative;
}

We need this because .position() gives us the position relative to an element's offset parent, and we can make #container the offset parent of the anchor tag by setting its position to relative.

JSFiddle: http://jsfiddle.net/divad12/RYPm7/1/

Also, for HTML5, you may want to consider setting an anchor tag's id attribute instead of name: HTML Anchors with 'name' or 'id'?

甜味拾荒者2024-12-25 03:26:01

不过,最好的方法是让链接通过 Ajax 加载完整的文章。

Still, the best way is to make the link load the full article via Ajax.

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