链接到页面底部附近的锚点
我正在做一些文档,其中大量使用锚点来链接 wiki 上的页面。
请参见此处:
http://code.google.com/p/xcmetadataservicestoolkit/wiki/ ServicesExplained#Platform_Data_Structures
真正使此功能正常运行的功能是浏览器在窗格的绝对顶部显示锚点。令人困惑的是,当链接到锚点时,由于页面一直向下滚动,因此锚点显示在页面的中间位置,
请参见此处:
我的wiki(第一个链接)中的解决方案是在页面底部放置一个空白图像,只是为了使浏览器在顶部显示锚点。有更好的方法吗?有没有办法在第二个链接中做到这一点(其中我无法添加空白图像)?
I'm doing some documentation where I make heavy use of anchors for linking between pages on a wiki.
see here:
http://code.google.com/p/xcmetadataservicestoolkit/wiki/ServicesExplained#Platform_Data_Structures
The feature that really makes this work well is when the browser shows the anchor at the absolute top of the pane. When it gets confusing is when linking to an anchor shows the anchor half-way down the page since the page is scrolled down all the way
see here:
My solution in the wiki (first link) was to put a blank image at the bottom of the page simply to make the browser show the anchor right at the top. Is there a better way to do this? Is there a way to do it in the second link (in which I can't add a blank image)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在页面底部放置空白图像是一个坏主意,因为它会将文档扩展到不必要的高度。
您可以添加一些 JavaScript 来对您刚刚到达的锚点应用效果,无论它在哪里都可以突出显示它。
Putting a blank image at the bottom of your page is a bad idea, since it will expand your document to a unnecessary height.
You could throw in some javascript to apply an effect to the anchor you just travelled to, to highlight it wherever it is.
如果不改变文档的高度(即在底部添加额外的填充),您将始终遇到此问题。
然而,使用一点 JS/jQuery,用户体验可以得到显着改善:
单击命名锚点时:
创建了一个演示来说明概念:http://jsfiddle.net/mrchief/PYsyN/9/
CSS
JS
Without altering the height of your document (i.e. adding extra padding at bottom), you'll always have this issue.
However, using bit of JS/jQuery, the user experience can be improved considerably:
On clicking a named anchor:
Created a demo to illustrate the concepts: http://jsfiddle.net/mrchief/PYsyN/9/
CSS
JS
您可以使用以下 CSS 创建一个绝对定位的伪元素,其目标块的高度很高(对于帖子中的第二个链接:
高度必须在视口的高度左右,因此最好的解决方案是创建这些样式但如果你不想使用 js,只需使用
height: 1000px
或更高 — 当然,如果你不介意底部有间隙的话最好 。部分:它是只有CSS,并且当没有定位锚点时不会有间隙
编辑:只是预览一下:如果vw/vh 单位将出现在其他浏览器中(现在仅在 IE9 中),这可以通过使用
height 的 CSS 来完成: 100vh :)
You can create a absolutre positioned pseudo-element with a great height to targeted block using just the following CSS (for the second link in your post:
The height must be around the height of the viewport, so the best solution is to create these styles on the fly using js. But if you don't wan't to use js, just use
height: 1000px
or more — if you don't mind a gap at the bottom of course.The best part: it's only CSS and there would be no gap when no anchors are targeted.
Edit: just a sneak peek into the future: if the vw/vh units would come to other browsers (now it's only in IE9), this could be awesomely done with just CSS using
height: 100vh
:)您可以使用Javascript/jQuery创建一个白色的
div
,它具有将元素放置在浏览器窗口顶部所需的必要高度,您甚至可以将其删除滚动离开。不过,我强烈建议不要这样做,因为这会在不需要的地方扩展您的页面。更聪明的做法是简单地在到达那里时设置标签样式(通过 Javascript / jQuery),以便它向查看者弹出,例如通过将
font-weight
设置为粗体或更改背景-颜色
。You could use Javascript / jQuery to create a white
div
that has the necessary height needed to put your element at the top of the browser window, and you could even remove this upon scrolling away.However I would highly recommend against doing so as this will expand your page where it isn't needed. It's a lot smarter to simply style the tag upon going there (through Javascript / jQuery) so it pops out to the viewer, for instance by setting the
font-weight
to bold or changing thebackground-color
.我可能会结合使用 jQuery 和 PHP 来实现此目的:
PHP(在
元素之后的某个位置):
然后是 jQuery:
希望这会有所帮助。
I would probably use a combination of jQuery and PHP for this:
PHP(somewhere right after your
<body>
element):And then the jQuery:
Hope this helps.