javascriptjqueryscrolltopanelstabbed

Scroll to an anchor tag when panel tab selected


Is there a way to scroll to an anchor on a page when a tab on a tabbed panel is selected? I would like the tabbed panel to scroll to the top of the browser window.

My development page is here.

The code for the anchor and tabs is:

<h1 class="heading">
    <a name="heading" id="heading"></a>Asia &amp; South East Asia International Schools
</h1>
<div id="TabbedPanels1" class="TabbedPanels">
    <ul class="TabbedPanelsTabGroup">
        <li class="TabbedPanelsTab" tabindex="0">Overview</li>
        <li class="TabbedPanelsTab" tabindex="0">Activity Weeks</li>
        <li class="TabbedPanelsTab" tabindex="0">Expeditions</li>
        <li class="TabbedPanelsTab" tabindex="0">Testimonials</li>
    </ul>

</div>

Any help would be greatly appreciated. Cheers


Solution

  • You might find scrollIntoView useful.

    There are a couple of problems with using scrollIntoView:

    You can implement scrollTo on your current tab-panel using something like this:

    <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>
    

    Your question is tagged jquery but you don't appear to be using it. jquery provides a much more powerful set of scrolling routines, see this jquery demo page.

    Edit: It looks like Spry has a problem with the approach above: adding an onclick event to the list elements is breaking the Spry tab-panel. I only have marginal experience with Spry, but this Spry.Utils method looks like it might be part of a Spry solution -

    You'll need to include SpryUtils in your page, then you'd need to add javascript something like the following - sorry, but I can't try this on my machine, so this is untested:

    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);