2016-08-18 11:27:37 +08:00
|
|
|
---
|
2016-10-30 15:11:22 +08:00
|
|
|
order: 2
|
2016-09-01 11:04:34 +08:00
|
|
|
title:
|
|
|
|
zh-CN: 滚动容器
|
|
|
|
en-US: Container to scroll.
|
2016-08-18 11:27:37 +08:00
|
|
|
---
|
|
|
|
|
2016-09-01 11:04:34 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-08-18 11:27:37 +08:00
|
|
|
用 `target` 设置 `Affix` 需要监听其滚动事件的元素,默认为 `window`。
|
|
|
|
|
2016-09-01 11:04:34 +08:00
|
|
|
## en-US
|
|
|
|
|
2016-10-30 15:11:22 +08:00
|
|
|
Set a `target` for 'Affix', which is listen to scroll event of target element (default is `window`).
|
2016-09-01 11:04:34 +08:00
|
|
|
|
2020-01-03 11:55:35 +08:00
|
|
|
```tsx
|
2016-08-18 11:27:37 +08:00
|
|
|
import { Affix, Button } from 'antd';
|
2022-05-21 22:14:15 +08:00
|
|
|
import React, { useState } from 'react';
|
2016-08-18 11:27:37 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
2020-12-17 15:09:18 +08:00
|
|
|
const [container, setContainer] = useState<HTMLDivElement | null>(null);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
2020-01-03 11:55:35 +08:00
|
|
|
return (
|
|
|
|
<div className="scrollable-container" ref={setContainer}>
|
|
|
|
<div className="background">
|
|
|
|
<Affix target={() => container}>
|
|
|
|
<Button type="primary">Fixed at the top of container</Button>
|
|
|
|
</Affix>
|
2016-08-18 11:27:37 +08:00
|
|
|
</div>
|
2020-01-03 11:55:35 +08:00
|
|
|
</div>
|
2020-01-06 11:13:39 +08:00
|
|
|
);
|
2020-01-03 11:55:35 +08:00
|
|
|
};
|
2016-08-18 11:27:37 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|
2016-10-30 15:11:22 +08:00
|
|
|
|
|
|
|
<style>
|
2016-12-03 16:19:22 +08:00
|
|
|
#components-affix-demo-target .scrollable-container {
|
2016-10-30 15:11:22 +08:00
|
|
|
height: 100px;
|
|
|
|
overflow-y: scroll;
|
|
|
|
}
|
|
|
|
#components-affix-demo-target .background {
|
2016-12-03 16:19:22 +08:00
|
|
|
padding-top: 60px;
|
2016-10-30 15:11:22 +08:00
|
|
|
height: 300px;
|
|
|
|
background-image: url('https://zos.alipayobjects.com/rmsportal/RmjwQiJorKyobvI.jpg');
|
|
|
|
}
|
|
|
|
</style>
|