TomVista
V2EX  ›  问与答

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

  •  
  •   TomVista · Dec 16, 2019 · 1860 views
    This topic created in 2538 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   ·   2089 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 08:14 · PVG 16:14 · LAX 00:14 · JFK 03:14
    ♥ Do have faith in what you're doing.