Skip to content

元素锚点滚动实现

元素是否在容器内判断

元素底部大于等于容器顶部并且元素顶部小于等于容器底部,则元素在容器内。

ts
const containerRect = {
  top: 50,
  bottom: 300,
}
const rect = {
  top: 20,
  bottom: 200,
}

if (rect.bottom >= containerRect.top && rect.top <= containerRect.bottom) {
  // 在容器内
}