About using offsettop in DOM to obtain the position of the current element from the browser window

Time:2021-8-29

Offsettop is the distance from the outer border of the child element to the inner border of the parent element
Directly relative to the body value, you need to look up layer by layer
What is helpful to you in the following code is the code in while, and the outer layer is some records for yourself

var table = $('.tableTemplate')
      var top = 0
      for (var i = 0; i < table.length; i++) {
        if (table[i].offsetTop !== 0) {
          console.log('table.parentNode', table[i].parentNode)
          let par = table[i].offsetParent
          top = table[i].offsetTop
          console.log('1', table[i].offsetTop)
          While (PAR) {// loop to get the height between the current object and the body
            console.log('table.parentNode', par, par.offsetTop)
            top += par.offsetTop
            par = par.offsetParent
          }
        }
      }

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]