Tabs automatically become scrollable when there are too many to fit on screen
测试 1
测试 2
测试 3
测试 4
测试 5
测试 6
测试 7
测试 8
测试 9
测试 10
测试 11
<div class="row">
<div class="col s12">
<ul class="tabs">
<li class="tab col s3"><a href="#test1">测试 1</a></li>
<li class="tab col s3"><a class="active" href="#test2">测试 2</a></li>
<li class="tab col s3 disabled"><a href="#test3">Disabled Tab</a></li>
<li class="tab col s3"><a href="#test4">测试 4</a></li>
</ul>
</div>
<div id="test1" class="col s12">测试 1</div>
<div id="test2" class="col s12">测试 2</div>
<div id="test3" class="col s12">测试 3</div>
<div id="test4" class="col s12">测试 4</div>
</div>
初始化
var instance = M.Tabs.init(el, options);
// 或用JQuery
$(document).ready(function(){
$('.tabs').tabs();
});
选项
名称 | 类型 | 默认 | 描述 |
---|---|---|---|
duration | Number | 300 | Transition duration in milliseconds. |
onShow | Function | null | Callback for when a new tab content is shown. |
swipeable | Boolean | false | Set to true to enable swipeable tabs. This also uses the responsiveThreshold option. |
responsiveThreshold | Number | Infinity | The maximum width of the screen, in pixels, where the swipeable functionality initializes. |
方法
Because jQuery is no longer a dependency, all the methods are called on the plugin instance. You can get the plugin instance like this:
var instance = M.Tabs.getInstance(elem); /* JQuery方法调用 您仍然可以使用旧的jQuery插件方法调用。 但您将无法访问实例属性。 $('.tabs').tabs('方法名称'); $('.tabs').tabs('方法名称', 参数名称); */
.select();
Show tab content that corresponds to the tab with the id
Arguments
String: The id of the tab that you want to switch to
instance.select('tab_id');
.updateTabIndicator();
Recalculate tab indicator position. This is useful when the indicator position is not correct.
instance.updateTabIndicator();
.destroy();
销毁插件实例并回收
instance.destroy();
属性
名称 | 类型 | 描述 |
---|---|---|
el | Element | 插件初始化时使用的DOM元素。 |
options | Object | The options the instance was initalized with. |
index | Number | The index of tab that is currently shown. |
预选标签
By default, the first tab is selected. But if this is not what you want, you can preselect a tab by either passing in the hash in the url ex:#test2
. Or you can add the class active
to the a
tag.
<li class="tab"><a class="active" href="#test3">测试 3</a></li>
链接到外部页面
By default, Materialize tabs will ignore their default anchor behaviour. To force a tab to behave as a regular hyperlink, just specify the target
property of that link! A list of target
values may be found here.
<li class="tab col s2">
<a target="_blank" href="https://github.com/Dogfalo/materialize">External link in new window</a>
</li>
<li class="tab col s2">
<a target="_self" href="https://github.com/Dogfalo/materialize">External link in same window</a>
</li>
可滑动标签
By setting the swipeable
option to true
, you can enable tabs where you can swipe on touch enabled devices to switch tabs. Make sure you keep the tab content divs in the same wrapping container. You can also set the responsiveThreshold
option to a screen width in pixels where the swipeable functionality will activate.
Note: This is also touch compatible! Try swiping with your finger to scroll through the carousel.
<ul id="tabs-swipe-demo" class="tabs">
<li class="tab col s3"><a href="#test-swipe-1">测试 1</a></li>
<li class="tab col s3"><a class="active" href="#test-swipe-2">测试 2</a></li>
<li class="tab col s3"><a href="#test-swipe-3">测试 3</a></li>
</ul>
<div id="test-swipe-1" class="col s12 blue">测试 1</div>
<div id="test-swipe-2" class="col s12 red">测试 2</div>
<div id="test-swipe-3" class="col s12 green">测试 3</div>
固定宽度标签
Add the .tabs-fixed-width
class to the tabs container
测试 1
测试 2
测试 3
测试 4
测试 5
<ul class="tabs tabs-fixed-width tab-demo z-depth-1">
<li class="tab"><a href="#test1">测试 1</a></li>
<li class="tab"><a class="active" href="#test2">测试 2</a></li>
<li class="tab disabled"><a href="#test3">Disabled Tab</a></li>
<li class="tab"><a href="#test4">测试 4</a></li>
<li class="tab"><a href="#test0">测试 5</a></li>
</ul>
<div id="test1" class="col s12"><p>测试 1</p></div>
<div id="test2" class="col s12"><p>测试 2</p></div>
<div id="test3" class="col s12"><p>测试 3</p></div>
<div id="test4" class="col s12"><p>测试 4</p></div>
<div id="test0" class="col s12"><p>测试 5</p></div>