您的位置:首页技术文章
文章详情页

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

【字号: 日期:2022-12-25 13:35:55浏览:70作者:猪猪

问题描述

这里的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

有理有据,令人信服!

问题解答

回答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 更慢。

标签: JavaScript