search

Home  >  Q&A  >  body text

javascript - A piece of code about iscroll, I hope someone can explain it

Hereif (el instanceof SVGElement)The intention should be a capability test, but I found that the incoming element el, whether there is SVGElement on its prototype chain and whether it has getBoundingClientRect seems to have nothing to do with it. Relationship, right? Even if there is no SVGElement in the prototype chain of an element, it still has the getBoundingClientRect method. So what does the writing here mean?

    me.getRect = function(el) {

        if (el instanceof SVGElement) {
            var rect = el.getBoundingClientRect();
            return {
                top : rect.top,
                left : rect.left,
                width : rect.width,
                height : rect.height
            };
        } else {
            
            return {
                top : el.offsetTop,
                left : el.offsetLeft,
                width : el.offsetWidth,
                height : el.offsetHeight
            };
        }
    };

grateful!

CRIMX

Well-founded and convincing!

世界只因有你世界只因有你2860 days ago744

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:36:55

    1. SVGElement - The properties offsetParent, offsetTop, offsetLeft, offsetWidth, and offsetHeight are deprecated in Chrome 48.

    2. The offsetLeft and offsetTop properties of SVG elements always returns 'undefined'.

    The reason

    is not used for HTMLElement may be due to the fact that getBoundingClientRect is slower.

    reply
    0
  • Cancelreply