React实现div随页面滚动而固定在浏览器顶部

代码
export default class FixedHeader extends React.Component {
  constructor(props) {
    super(props);
    this.state = { needFixed: false };
  }

  componentDidMount() {
    window.addEventListener('scroll', this.handleScroll);
  }

  handleScroll = event => {
    // 滚动条滚动高度
    const scrollTop = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
    console.log(scrollTop);
    const fixedTop = document.getElementById('components1').offsetTop;
    if (scrollTop >= fixedTop) {
      this.setState({ needFixed: true });
      console.log({ needFixed: true });
    } else if (scrollTop < fixedTop) {
      this.setState({ needFixed: false });
      console.log({ needFixed: false });
    }
  };
 render() {
    const { needFixed } = this.state;
    const anchorHeaderStyle = needFixed ? { position: 'fixed', top: 0, zIndex: 9 } : {};
    return (
      <PageLayout>
      <div>此处为页面的其他部分元素,反正很多<div>
<div className={styles.anchorHeader} id="anchorHeader" style={anchorHeaderStyle}>
          <div className={styles.anchorContent}>
            <Anchor>
              <Link href="#components1" title="典型业务场景" />
              <Link href="#components2" title="应用案例" />
            </Anchor>
          </div>
        </div>
<div className={styles.content} id="components1">典型业务场景</div>
<div className={styles.content} id="components1">客户案例</div>


      </PageLayout>
    );
  }
}

实现效果

在这里插入图片描述

由于使用的antd的组件,对antd组件的样式进行改造

原来的Anchor锚点组件
在这里插入图片描述

显然不符合我的需要,故根据业务需求进行修改为横向的


.anchorHeader {
  margin: 0 auto;
  width: 100%;
  background-color: #fff;
}

.anchorContent {
  margin: 0 auto;
  min-width: 255px;
  max-width: 255px;
}

.anchorContent :global(.ant-anchor-link) {
  padding: 7px 0 7px 16px;
  line-height: 1.143;
  display: inline-block;
}

.anchorContent :global(.ant-anchor) {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  color: rgba(0, 0, 0, 0.65);
  font-size: 25px;
  font-variant: tabular-nums;
  line-height: 1.5;
  list-style: none;
  font-feature-settings: 'tnum';
  position: relative;
  padding-left: 2px;
}

.anchorContent :global(.ant-anchor-wrapper) {
  margin-left: -4px;
  padding-left: 4px;
  overflow: auto;
  background-color: #fff;
  height: 50px;
}

.anchorContent :global(.ant-anchor-ink) {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  display: none;
}

.anchorContent :global(.ant-anchor-link > .ant-anchor-link-title) {
  display: block;
  padding-bottom: 3px;
  border-bottom: 1px solid #faf3f5;
}

.anchorContent :global(.ant-anchor-link-active > .ant-anchor-link-title) {
  color: #1890ff;
  display: block;
  padding-bottom: 3px;
  border-bottom: 1px solid #1890ff;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuyunshengsir

微信:lys20191020

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值