关于iscroll的一段代码,希望有人解释

这里的if (el instanceof SVGElement)用意应该是一个能力检测,但是我发现传入的元素el,他的原型链上是否有SVGElement和它是否具有getBoundingClientRect好像并没有什么关系吧?即使一个元素的原型链上没有SVGElement,它还是有getBoundingClientRect方法的,那这里的写法到底是什么意思呢?

    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

            };

        }

    };

感谢!

CRIMX

有理有据,令人信服!


泛舟湖上清波郎朗
浏览 530回答 1
1回答

守着星空守着你

SVGElement - The properties offsetParent, offsetTop, offsetLeft, offsetWidth, and offsetHeight are deprecated in Chrome 48.The offsetLeft and offsetTop properties of SVG elements always returns 'undefined'.不对 HTMLElement 使用的原因可能是考虑到 getBoundingClientRect 更慢。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript