您可能会发现scrollIntoView 很有用。
使用scrollIntoView有几个问题:
- 它将所选元素一直滚动到屏幕顶部。如果所选元素上方有一点空间,而不是将其一直推到窗口顶部,看起来会更好。您可以通过将
scrollIntoView 与scrollBy 结合使用来解决此问题,后者可以相对滚动。
- 在您的特定站点上,一些选项卡片部分非常短,因此,例如,在选择概述 em>部分时 - 一个简短的 - 没有空间滚动到顶部屏幕大小,取决于窗口大小。但是,较长的部分会将标签面板滚动到顶部。这种行为差异看起来不太好。
您可以使用以下方式在当前的选项卡面板上实现 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 提供了一组更强大的滚动例程,请参阅this jquery demo page。
编辑: 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);