2018-08-28 18:56:25 +08:00
|
|
|
---
|
|
|
|
order: 12
|
|
|
|
title:
|
|
|
|
zh-CN: 自定义页签头
|
|
|
|
en-US: Customized bar of tab
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
使用 react-sticky 组件实现吸顶效果。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2019-08-07 17:26:39 +08:00
|
|
|
Use react-sticky.
|
2018-08-28 18:56:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
|
|
|
import type { TabsProps } from 'antd';
|
2018-08-28 18:56:25 +08:00
|
|
|
import { Tabs } from 'antd';
|
2022-05-23 14:37:16 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { Sticky, StickyContainer } from 'react-sticky';
|
2018-08-28 18:56:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const renderTabBar: TabsProps['renderTabBar'] = (props, DefaultTabBar) => (
|
2018-08-28 18:56:25 +08:00
|
|
|
<Sticky bottomOffset={80}>
|
|
|
|
{({ style }) => (
|
2019-12-24 20:58:42 +08:00
|
|
|
<DefaultTabBar {...props} className="site-custom-tab-bar" style={{ ...style }} />
|
2018-08-28 18:56:25 +08:00
|
|
|
)}
|
|
|
|
</Sticky>
|
|
|
|
);
|
|
|
|
|
2022-08-05 10:49:08 +08:00
|
|
|
const items = new Array(3).fill(null).map((_, i) => {
|
|
|
|
const id = String(i + 1);
|
|
|
|
return {
|
|
|
|
label: `Tab ${id}`,
|
|
|
|
key: id,
|
|
|
|
children: `Content of Tab Pane ${id}`,
|
|
|
|
style: i === 0 ? { height: 200 } : undefined,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => (
|
2018-08-28 18:56:25 +08:00
|
|
|
<StickyContainer>
|
2022-08-05 10:49:08 +08:00
|
|
|
<Tabs defaultActiveKey="1" renderTabBar={renderTabBar} items={items} />
|
2022-04-03 23:27:45 +08:00
|
|
|
</StickyContainer>
|
2018-11-28 15:00:03 +08:00
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|
2019-12-24 20:58:42 +08:00
|
|
|
|
|
|
|
```css
|
|
|
|
.site-custom-tab-bar {
|
|
|
|
z-index: 1;
|
|
|
|
background: #fff;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
<style>
|
|
|
|
[data-theme="dark"] .site-custom-tab-bar {
|
|
|
|
background: #141414;
|
|
|
|
}
|
|
|
|
</style>
|