Вы можете использовать плагин jQuery PositionCalculator .
JavaScript:
jQuery(function($) {
var count = 1;
var $popup = $('#popup');
function showPopup(event) {
// reset position
$popup.show().css({top:0, left:0});
// calculate new position
var calculator = new $.PositionCalculator({
item: $popup,
itemAt: "bottom left",
target: this,
targetAt: "top right",
flip: "both"
});
var posResult = calculator.calculate();
// set new position
$popup.css({
top: posResult.moveBy.y + "px",
left: posResult.moveBy.x + "px"
});
}
// initial add event handler
$('.has_popup').on('click', showPopup);
$popup.on('click', function() { $popup.hide(); });
});
HTML:
<input type="text" class="has_popup" value="click me"/>
<div id="popup" style="display:none; position:absolute; padding:10px; background:rgba(0,0,0,0.8); color:#ffffff">
I am the popup. <b>Click me to close</b>
</div>
Вот пример в JSBin