Skip to content

Instantly share code, notes, and snippets.

@coodoo
Last active December 17, 2015 20:29
Show Gist options
  • Select an option

  • Save coodoo/5667953 to your computer and use it in GitHub Desktop.

Select an option

Save coodoo/5667953 to your computer and use it in GitHub Desktop.
- Adjust bootstrap's popover arrow position to always be pointing as close as possible to the target (for example, a div or p element) - demo http://cl.ly/image/3Z02121G3G0P
var $foo = this.$('#foo').popover( o );
// 調整 popover 的 arrow 位置指向 label 中央
$foo.on('shown', function( evt ){
// 取得文字 label 的中間位置
var rect = $foo[0].getBoundingClientRect();
var targetPos = rect.left + rect.width/2;
// 找到 arrow 元件
var $arrow = $foo.data('popover').$tip.find('.arrow');
// 取得 popover 面板本身寬度
var popoverWidth = $arrow.parent().get(0).getBoundingClientRect().width;
// 移動 arrow 位置到 label 中央
$arrow.css('left', Math.min( targetPos, popoverWidth*0.9 ) );
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment