TomVista
V2EX  ›  问与答

帮看下这段 js 代码是干啥的

  •  
  •   TomVista · Dec 16, 2019 · 1838 views
    This topic created in 2524 days ago, the information mentioned may be changed or developed.
    function offset(el) {
      let left = 0
      let top = 0
    
      while (el) {
        left -= el.offsetLeft
        top -= el.offsetTop
        el = el.offsetParent
      }
    
      return {
        left,
        top,
      }
    }
    

    +号求得是 el 到 body 元素的偏移距离

    -号是啥玩意.....

    2 replies  •  2019-12-16 17:51:50 +08:00
    imherer
    imherer
    Dec 16, 2019
    1
    不都是求和么?只不过一个正一个负
    TomVista
    TomVista
    OP
    Dec 16, 2019
    2
    @imherer 我没算初始值 0 ........... :dog
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1804 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 16:11 · PVG 00:11 · LAX 08:11 · JFK 11:11
    ♥ Do have faith in what you're doing.