javascript - A piece of code about iscroll, I hope someone can explain it
世界只因有你
世界只因有你 2017-05-16 13:34:55
0
1
684

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!

世界只因有你
世界只因有你

reply all(1)
伊谢尔伦
  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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template