ant-design/components/affix/demo/target.md

47 lines
1.1 KiB
Markdown
Raw Normal View History

---
2016-10-30 15:11:22 +08:00
order: 2
title:
zh-CN: 滚动容器
en-US: Container to scroll.
---
## zh-CN
`target` 设置 `Affix` 需要监听其滚动事件的元素,默认为 `window`
## 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`).
```tsx
2020-01-22 12:11:49 +08:00
import React, { useState } from 'react';
import { Affix, Button } from 'antd';
2020-01-21 17:14:58 +08:00
const Demo: React.FC = () => {
const [container, setContainer] = useState<HTMLDivElement | null>(null);
return (
<div className="scrollable-container" ref={setContainer}>
<div className="background">
<Affix target={() => container}>
<Button type="primary">Fixed at the top of container</Button>
</Affix>
</div>
</div>
2020-01-06 11:13:39 +08:00
);
};
ReactDOM.render(<Demo />, mountNode);
2019-05-07 14:57:32 +08:00
```
2016-10-30 15:11:22 +08:00
<style>
#components-affix-demo-target .scrollable-container {
2016-10-30 15:11:22 +08:00
height: 100px;
overflow-y: scroll;
}
#components-affix-demo-target .background {
padding-top: 60px;
2016-10-30 15:11:22 +08:00
height: 300px;
background-image: url('https://zos.alipayobjects.com/rmsportal/RmjwQiJorKyobvI.jpg');
}
</style>