js A相对b的位置和卷动

凌得涂

关注

阅读 60

2022-04-29

 

function heightToTop(ele){
    //ele为指定跳转到该位置的DOM节点
    const root = document.body;
    let height = 0;
    do{
        height += ele.offsetTop;
        ele = ele.offsetParent;
    }while( ele !== root )
    return height;
}

元素滚动到可见区域  // var ele = document.getElementById("h3")
    // window.scrollTo({
    //     top:heightToTop(ele),
    //     behavior:'smooth'
    // })
   // 方法2
//    ele.scrollIntoView({behavior:'smooth'})

精彩评论(0)

0 0 举报