V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
TomVista
V2EX  ›  问与答

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

  •  
  •   TomVista · Dec 16, 2019 · 1472 views
    This topic created in 2323 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
        1
    imherer  
       Dec 16, 2019
    不都是求和么?只不过一个正一个负
    TomVista
        2
    TomVista  
    OP
       Dec 16, 2019
    @imherer 我没算初始值 0 ........... :dog
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6086 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 02:07 · PVG 10:07 · LAX 19:07 · JFK 22:07
    ♥ Do have faith in what you're doing.