您可能会发现scrollIntoView很有用。
使用有几个问题scrollIntoView
:
- 它将所选元素一直滚动到屏幕顶部。如果所选元素上方有一点空间,而不是将其一直推到窗口顶部,看起来会更好。您可以通过结合 scrollBy 来解决这个
scrollIntoView
问题,它可以滚动一个相对量。
- On your particular site some of the tabbed sections are pretty short, so, for instance, when selecting the Overview section - a short one - there's no room to scroll the tab-panel to the top of the screen, depending on the window size. 但是,较长的部分会将选项卡面板滚动到顶部。这种行为上的差异看起来不太好。
您可以使用以下内容在当前选项卡面板上实现 scrollTo :
<ul class="TabbedPanelsTabGroup">
<li id="tabHeaderOverview" class="TabbedPanelsTab" tabindex="0" onclick="document.getElementById('tabHeaderOverview').scrollIntoView()">Overview</li>
<li id="tabHeaderActivity" class="TabbedPanelsTab" tabindex="0" onclick="document.getElementById('tabHeaderActivity').scrollIntoView()">Activity Weeks</li>
<li id="tabHeaderExpeditions" class="TabbedPanelsTab" tabindex="0" onclick="document.getElementById('tabHeaderExpeditions').scrollIntoView()">Expeditions</li>
<li id="tabHeaderTestimonials" class="TabbedPanelsTab TabbedPanelsTabSelected" tabindex="0" onclick="document.getElementById('tabHeaderTestimonials').scrollIntoView()">Testimonials</li>
</ul>
您的问题被标记为 jquery,但您似乎没有使用它。jquery 提供了一组更强大的滚动例程,请参阅这个 jquery 演示页面。
编辑:看起来 Spry 对上述方法有问题:向列表元素添加 onclick 事件会破坏 Spry 选项卡面板。我对 Spry 的经验很少,但这个Spry.Utils方法看起来可能是 Spry 解决方案的一部分 -
您需要在页面中包含SpryUtils,然后您需要添加类似以下内容的 javascript - 抱歉,但我无法在我的机器上尝试此操作,因此未经测试:
function scrollTabHeader(event)
{
this.scrollIntoView();
// this incorporates the scrollBy mentioned above:
window.scrollBy(0, -10);
}
Spry.Utils.addEventListener("tabHeaderOverview", "click", scrollTabHeader, false);
Spry.Utils.addEventListener("tabHeaderActivity", "click", scrollTabHeader, false);
Spry.Utils.addEventListener("tabHeaderExpeditions", "click", scrollTabHeader, false);
Spry.Utils.addEventListener("tabHeaderTestimonials", "click", scrollTabHeader, false);