javascript - 關(guān)于iscroll的一段代碼,希望有人解釋
問題描述
這里的if (el instanceof SVGElement)用意應(yīng)該是一個能力檢測,但是我發(fā)現(xiàn)傳入的元素el,他的原型鏈上是否有SVGElement和它是否具有g(shù)etBoundingClientRect好像并沒有什么關(guān)系吧?即使一個元素的原型鏈上沒有SVGElement,它還是有g(shù)etBoundingClientRect方法的,那這里的寫法到底是什么意思呢?
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
有理有據(jù),令人信服!
問題解答
回答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 更慢。
