javascript - Get element offset relative to document without getBoundingClientRect (Webkit bug) -
positioning tooltip / popup case of using getboundingclientrect() or jquery's offset() function (which internally uses getboundingclientrect) offset relative document or window, if zoom in on webkit browsers, these functions start returning unexpected / wrong results because getboundingclientrect function implemented weirdly in webkit. has been issue forever , reason has never been fixed.
question: how can (if possible) calculate exact pixel offset of element relative document without using getboundingclientrect() values don't change on zoom?
i able solve in https://jsbin.com/vaculorita/1/edit?css,js,output
i'm not sure jquery's offset trying combination of switching absolute positioning , manually using getboundingclientrect works me.
html
<div id="tooltip">tooltip</div> <input type="text" id="username">
css
#username { margin-left: 54px; margin-top: 21px; } #tooltip { position: absolute; left: 0; top:0; }
js
$(function() { function movetooltip() { var $tooltip = $('#tooltip').first(); var $input = $('#username').first(); var rect = $input.get(0).getboundingclientrect(); $tooltip.css('left', rect.left + $input.width()); $tooltip.css('top', rect.top + $input.height()); } movetooltip(); $(window).on('resize scroll', movetooltip); });
Comments
Post a Comment