通过命名锚点导航长选项卡面板时,jQuery 选项卡滚动到视图之外
看看这个小提琴:
http://jsfiddle.net/contendia/GBTAS/
这个想法是仅在 js 可以运行时创建选项卡,以保持页面对非 js 用户友好。否则,我想将页面呈现为一堆 div。选项卡列表完全自动化,具体取决于选项卡数量,并且每个选项卡根据相应的 div id 属性命名。
一切都运行良好,但现在我正在尝试重新设计单击事件以更普遍地导航选项卡。之前,我只是在列表项选项卡之一 (li) 上查找单击事件,然后运行必要的代码来更改选项卡。
但现在我想让这个函数更加通用,这样当用户单击哈希值等于选项卡 ID 之一的任何锚点时它就可以工作,而无需定位实际选项卡。我不确定如何在锚点中定位哈希并根据选项卡 ID 对其进行测试。我得到的功能在 FF 和 IE 中都可以用于单击选项卡和单击锚链接(例如,Tab 1
)。但是,如果选项卡比视口长,则选项卡顶部会滚动到窗口顶部,导致选项卡链接消失。用户必须手动将选项卡链接滚动回视图中才能导航到另一个选项卡。我尝试过 .scrollTo() 将页面移回原位,但没有成功。
这只是 .scrollTo 的问题还是我对点击功能的处理完全错误?
将不胜感激任何想法。奖金,如果它能动画就太好了。
Take a look at this fiddle:
http://jsfiddle.net/contendia/GBTAS/
The idea is to keep the page friendly for non-js users by only creating tabs when js can run. Otherwise, I want to present the page as just a stack of divs. The list for the tabs is completely automated depending on the number of tabs and each tab is named according to the corresponding divs id attribute.
All is working well, but now I'm attempting to rework the click event to navigate the tabs more generally. Before, I just looked for a click event on one of the list item tabs (li) and ran the necessary code to change the tabs.
But now I want to make this function more general so it works when a user clicks on any anchor with a hash value equal to one of the tabs IDs without having to target an actual tab. I'm not sure exactly how to go about targeting a hash in an anchor and testing it against the tab IDs. The function I've got works in FF and IE for both clicking a tab and clicking an anchor link (e.g., <a href="#tab1">Tab 1</a>
). However, if the tab is longer than the veiwport, the top of the tab scrolls to the top of the window, causing the tab links to disappear. The user must manually scroll the tab links back into view to navigate to another tab. I've tried .scrollTo() to move the page back into place, but with no luck.
Is this just an issue with .scrollTo or have I gone about the click function all wrong?
Would appreciate any ideas. Bonus, would be great if it would animate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,给你......我听说这对你不起作用,但对我来说非常有效。这是点击处理程序,当点击选项卡链接时会触发,它会添加哈希值,如您所见。如果没有“scrollTo()”,滚动就像您所描述的那样......有了它,问题就消失了。我得出的结论是,由于某种原因设置 href 会滚动浏览器视口。
Ok, here you go.. I hear you that this doesn't work for you, but it works perfectly for me. Here's the click handler which fires when they hit a tab link, and it adds the hash as you can see. Without the "scrollTo()" the scrolling is as you describe... with it, the problem goes away. I conclude then that setting the href for some reason scrolls the browser viewport.