文档加载时滚动到锚元素
过去有人发布过此内容,因此考虑到我很想看看是否有任何具有此功能的自定义脚本,我将重新发布它。我敢说,所以我不必自己编写代码:P
摘自 这篇文章: “我已经知道 jQuery 插件 ScrollTo,但到目前为止我还没有找到任何方法来实现以下目的:
用户访问我的网站(通过键入,而不是单击我页面上的链接)domain.com/bla。 php#foo
和锚点“#foo”存在现在我希望用户的浏览器不会自动滚动到“#foo”,而是希望平滑滚动以使元素“#foo”位于中间。视图的绝对顶部位置,而不是用户视图的绝对顶部位置,
谢谢!”
Someone posted this in the past so I'll re-post it considering I'd love to see if there's any custom scripts out there with this functionality. Dare I say, so I don't have to code it myself :P
Excerpt from this SO post:
"I already know the jQuery plugin ScrollTo, but I didn't find any way up to now to realize the following:
The users gets to my site (by typing, NOT by clicking a link on my page) domain.com/bla.php#foo
and the anchor "#foo" exists. Now I want that the browser of the user does NOT automatically scroll to "#foo", instead I want to smoothly scroll so that the element '#foo' is about in the middle of the view and NOT on the absolute top position of the users view.
Thanks so far!"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以破解这个问题的解决方案。在 中添加一个脚本以获取当前位置。将scrolltop 设置为页面顶部,然后在页面加载时添加 jQuery.ready(scrollTo) 块。
伪代码实现如下。
我希望这会在浏览器有机会捕捉到锚点之前触发。它可能有效,也可能无效。请随意充实自己。
You can hack the solution to this. Add a script right in the to grab the current location. Set the scrolltop to be the top of the page then add a jQuery.ready(scrollTo) block for when the page is loaded.
A pseudo-code implementation is as follows.
I hope that this will trigger before the browser has a chance to snap to the anchor. It may or may not work. Feel free to flesh it out yourself.