これは私の前の質問へのフォローアップです。
Bootstrap ツールチップを動的に配置する必要があります。そのためには、ツールチップをトリガーする要素の位置 (正常に動作する) と、表示されるはずのツールチップの幅を知る必要があります。
明確にするために編集: 私のツールチップは動的に生成され、コンテンツに挿入されます。たとえば、画面の左端に近すぎる場合は、「上」ではなく「右」に配置する必要があります。今、ツールチップの幅を取得して、実際に画面の外に出るときにのみ「右」に配置したいと考えています。
$('body').tooltip({
delay: { show: 300, hide: 0 },
selector: '[rel=tooltip]:not([disabled])',
placement: function(tip, element) {
var offsetLeft = $(element).offset().left;
var offsetRight = ( $(window).width() - ( offsetLeft + $(element).outerWidth() ) );
// need help here: how to get the width of the current tooltip?
// currently returns "0" for all elements
var tooltipWidth = $(tip).outerWidth();
console.log(tooltipWidth);
if (offsetLeft < 220) {
return 'right';
}
if (offsetRight < 220) {
return 'left';
}
else {
return 'top';
}
}
});
前もって感謝します。