0
点赞
收藏
分享

微信扫一扫

js A相对b的位置和卷动

凌得涂 2022-04-29 阅读 60
linqc#

 

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 条评论