使用URI哈希识别选项卡时阻止滚动

HNy*_*ard 11 jquery tabs jquery-ui jquery-ui-tabs fragment-identifier

我正在使用JQuery UI在我的应用程序中制作标签.我需要选项卡是可链接的(直接链接打开页面并选择正确的选项卡).这是通过使用散列标记/ 分段标识符来完成的.但是当选项卡上方和选项卡内部的内容很长时,我遇到了问题.

单击选项卡时,页面向下滚动到选项卡的开头.这不是我想要的.

我正在使用Jquery 1.7.1和Jquery UI 1.8.16.

javascript/Jquery代码是标准的Jquery UI选项卡,添加了事件"tabsshow".这在使用jquery ui标签更改location.hash(Stackoverflow问题)和JQuery UI标签时建议:点击标签时更新带有哈希的URL(博客 - 罗宾的技术日记)

$(document).ready(function() {
    $("#tabs").tabs();

    /**
     * Add hash to URL of the current page
     * 
     * http://chwang.blogspot.com/2010/02/jquery-ui-tabs-updating-url-with-hash.html
     * https://stackoverflow.com/questions/570276/changing-location-hash-with-jquery-ui-tabs
     */
    $("#tabs").bind('tabsshow',function(event, ui) {
        window.location.hash = ui.tab.hash;
    });
});
Run Code Online (Sandbox Code Playgroud)

以下HTML可用于测试行为

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
<div style="height: 400px;">Some other content</div>
<div id="tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
    <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
        <li class="ui-state-default ui-corner-top"><a href="#tab_1"><span>Tab 1</span></a></li>
        <li class="ui-state-default ui-corner-top"><a href="#tab_100"><span>Tab 100</span></a></li>
        <li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active"><a href="#tab_1000"><span>Tab 1000</span></a></li>
    </ul>

    <div id="tab_1" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
        <table style="height: 1000px"><tr><td>Hello. This is tab 1</td></tr></table>
    </div>


    <div id="tab_100" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
        <table style="height: 1000px"><tr><td>Hello. This is tab 100.</td></tr></table>
    </div>


    <div id="tab_1000" class="ui-tabs-panel ui-widget-content ui-corner-bottom"><h2>Heading</h2>
        <table style="height: 1000px"><tr><td>Hello. This is tab 1000.</td></tr></table>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

使用以下URL打开页面时,应打开选项卡1,而不是向下滚动到选项卡启动的位置.点击其中一个标签也是如此.

file.html#tab_1
Run Code Online (Sandbox Code Playgroud)

Mot*_*tie 5

这可能不是最好的方法,但如果在创建选项卡后重命名所有ID,则添加具有原始ID的哈希将不会滚动页面.我使用此方法是因为即使禁用了javascript,哈希也会将用户带到正确的ID.以下是代码的演示:

$("#tabs").tabs({
    create: function(event, ui) {
        // get tab plugin data
        var tabs = $('#tabs').data('tabs'),
            // tabs.anchors contains all of the tab anchors
            links = tabs.anchors;
        // tabs.panels contains each tab
        tabs.panels.each(function(i){
            // just adding a "mod_" prefix to every ID/hash
            this.id = 'mod_' + this.id;
            links[i].hash = '#' + this.id;
        });
    }
});

/**
 * Add hash to URL of the current page
 * 
 * http://chwang.blogspot.com/2010/02/jquery-ui-tabs-updating-url-with-hash.html
 * http://stackoverflow.com/questions/570276/changing-location-hash-with-jquery-ui-tabs
 */
$("#tabs").bind('tabsshow', function(event, ui) {
    // remove the prefix from the ID, so we're showing the original ID in the hash
    window.location.hash = ui.tab.hash.replace('mod_', '');
});
Run Code Online (Sandbox Code Playgroud)


Jam*_*son 5

正如其他人提到的,@Mottie 的代码可能曾经在旧版本的 jQuery UI 上工作过,但这肯定已经停止工作了。jQuery UI Tabs api 自编写以来发生了很大变化,所以这里是一个更新版本,至少适用于 jQuery 1.10.2

演示在这里:http : //jsfiddle.net/xsx5u5g2/

var $tabs = $("#tabs");
$tabs.tabs({
  create: function(event, ui) {
    // Adjust hashes to not affect URL when clicked.
    var widget = $tabs.data("uiTabs");
    widget.panels.each(function(i){
      this.id = "uiTab_" + this.id; // Prepend a custom string to tab id.
      widget.anchors[i].hash = "#" + this.id;
      $(widget.tabs[i]).attr("aria-controls", this.id);
    });
  },
  activate: function(event, ui) {
    // Add the original "clean" tab id to the URL hash.
    window.location.hash = ui.newPanel.attr("id").replace("uiTab_", "");
  },
});
Run Code Online (Sandbox Code Playgroud)


小智 5

我正在使用 jQuery 1.11.1。这对我来说很有效。

$("#tabs").tabs(); //initialize tabs
$(function() {
    $("#tabs").tabs({
        activate: function(event, ui) {
            var scrollTop = $(window).scrollTop(); // save current scroll position
            window.location.hash = ui.newPanel.attr('id'); // add hash to url
            $(window).scrollTop(scrollTop); // keep scroll at current position
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

jQuery UI 选项卡,单击不同选项卡时更新 url

感谢Jeff B将我指向这里http://jsfiddle.net/jtbowden/ZsUBz/44/