How do you get the rendered height of an element?

前端 未结 18 2712
情书的邮戳
情书的邮戳 2020-11-22 03:11

How do you get the rendered height of an element?

Let\'s say you have a

element with some content inside. This content inside is going to st
18条回答
  •  情书的邮戳
    2020-11-22 03:39

    If i understood your question correctly, then maybe something like this would help:

    function testDistance(node1, node2) {
        /* get top position of node 1 */
        let n1Pos = node1.offsetTop;
        /* get height of node 1 */
        let n1Height = node1.clientHeight;
        /* get top position of node 2 */
        let n2Pos = node2.offsetTop;
        /* get height of node 2 */
        let n2Height = node2.clientHeight;
    
        /* add height of both nodes */
        let heightTogether = n1Height + n2Height;
        /* calculate distance from top of node 1 to bottom of node 2 */
        let actualDistance = (n2Pos + n2Height) - n1Pos;
    
        /* if the distance between top of node 1 and bottom of node 2
           is bigger than their heights combined, than there is something between them */
        if (actualDistance > heightTogether) {
            /* do something here if they are not together */
            console.log('they are not together');
        } else {
            /* do something here if they are together */
            console.log('together');
        } 
    }
    

提交回复
热议问题