Affix 固钉
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
Guide
何时使用
当用户需要将某个组件固定在页面的某个位置时,可以使用 Affix 组件进行固定。
API
固钉
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 品牌样式前缀 | String | 'next-' |
container | 设置 Affix 需要监听滚动事件的容器元素签名:Function() => ReactElement返回值:{ReactElement} 目标容器元素的实例 | Function | () => window |
offsetTop | 距离窗口顶部达到指定偏移量后触发 | Number | - |
offsetBottom | 距离窗口底部达到制定偏移量后触发 | Number | - |
onAffix | 当元素的样式发生固钉样式变化时触发的回调函数签名:Function(affixed: Boolean) => void参数:affixed: {Boolean} 元素是否被固钉 | Function | () => {} |
className | 自定义样式类名 | String | - |
代码示例
也就意味着当页面往下滚动时,当 Affix 元素接触到浏览器边框时,此时会将 Affix 钉住。最简单的用法。默认情况下,Affix 的默认目标容器元素是整个 window
,并且 offsetTop = 0
,
查看源码在线预览
import { Affix, Button } from "@icedesign/base";
ReactDOM.render(
<div className="custom-affix-wrapper">
<Affix>
<Button type="secondary">Affixed Button</Button>
</Affix>
</div>,
mountNode
);
.custom-affix-wrapper {
padding: 40px 0;
}
可以通过 container
属性设置 Affix 组件需要监听其滚动事件的元素,该属性接收一个函数作为参数,默认为 () => window
。
查看源码在线预览
import { Affix, Button } from "@icedesign/base";
class Demo extends React.Component {
_containerRefHandler(ref) {
this.container = ref;
}
render() {
return (
<div
className="custom-affix-container"
ref={this._containerRefHandler.bind(this)}
>
<div className="a-wrapper">
<Affix
container={() => this.container}
offsetTop={0}
onAffix={affixed => console.log(affixed)}
>
<Button type="secondary">Affixed Button</Button>
</Affix>
</div>
</div>
);
}
}
ReactDOM.render(<Demo />, mountNode);
.custom-affix-container {
height: 150px;
overflow-y: scroll;
background: url(https://img.alicdn.com/tfs/TB1AbJXSpXXXXXJXpXXXXXXXXXX-32-32.jpg) repeat 50% 50%;
}
.custom-affix-container .a-wrapper {
padding-top: 50px;
height: 500px;
}
可以通过 offsetTop
或 offsetBottom
自定义偏移量。
查看源码在线预览
import { Affix, Button } from "@icedesign/base";
ReactDOM.render(
<div className="custom-affix-wrapper">
<Affix offsetBottom={0}>
<Button type="secondary">Affixed Button</Button>
</Affix>
</div>,
mountNode
);
.custom-affix-wrapper {
padding: 40px 0;
}