javascript - 關于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
有理有據,令人信服!
問題解答
回答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 更慢。
