js 获取dom元素距离顶部的位置

  // js 获取dom元素距离顶部的位置
    let divOneTop = document.getElementById('div-one').getBoundingClientRect().top
  
    let divTwoTop = document.getElementById('div-two').getBoundingClientRect().top

    let divThreeTop = document.getElementById('div-three').getBoundingClientRect().top


    // 监听浏览器窗口变化的方法函数
    window.onresize = function () {
        // 浏览器的可视区域高度
        let innerHeight = window.innerHeight
        let threeHeight = innerHeight - divThreeTop
        document.getElementById('div-three').style.height = threeHeight + 'px'
        document.getElementById('div-three').style.backgroundColor = 'red'
    }

 

posted @ 2022-09-28 15:12  敲代码改变不了中国  阅读(962)  评论(0编辑  收藏  举报