css,js 实现上滑覆盖页面

文章描述了如何使用CSS和JavaScript实现一个屏幕上滑时内容自动覆盖全屏的效果,关键在于`scroll`事件触发的`height`CSS过渡效果。
摘要由CSDN通过智能技术生成

屏幕录制


实现用户上滑,内容覆盖全屏,核心是css过渡效果。

<div className={styles.home}>
          <div className={`${styles.top}`}>Top Info</div>
          <div ref={this.rankRef} onScroll={this.handleScroll} className={`${styles.rank}  `}>
            <div ref={this.rankChildRef1} className={styles.rank1}>rank1</div>
            <div ref={this.rankChildRef2} className={styles.rank2}>rank2</div>
            <div ref={this.rankChildRef3} className={styles.rank3}>rank3</div>
          </div>
        </div>
        

.home {
  width: 100vw;
  min-height: 100vh;
  background: #f5f6f8;
}

.top {
  width: 100%;
  height: 50vh;
  background: #f4eee7;
}

.rank {
  position: fixed;
  width: 100vw;
  height: 60vh;
  bottom: 0;
  left: 0;
  background: #eed8cc;
  border-radius: 20px 20px 0 0;
  text-align: center;
  overflow-y: auto;
  transition: height 0.3s ease;
}

.rank1,
.rank2,
.rank3 {
  width: 100%;
  height: 50vh;
}

.rank1 {
  background: #d8e6c8;
}

.rank2 {
  background: #c4d7b3;
}

.rank3 {
  background: #a0bb8a;
}



handleScroll = () => {
    // 处理滚动事件
    // console.log('f', this.rankRef.current.getBoundingClientRect());
    // console.log('c1', this.rankChildRef1.current.getBoundingClientRect());
    // console.log('c2', this.rankChildRef2.current.getBoundingClientRect());
    // console.log('c3', this.rankChildRef3.current.getBoundingClientRect());

    const rankRef = this.rankRef.current;
    const ft = this.rankRef.current.scrollTop
    const c1t = this.rankChildRef1.current.scrollTop
    const c2t = this.rankChildRef2.current.scrollTop

    console.log(ft, c1t, c2t);

    if (ft > 0) { rankRef.style.height = '100vh'; }
    else { rankRef.style.height = '70vh'; }
  }; 
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值