最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get width of tooltip and calculate placement (Twitter Bootstrap) - Stack Overflow

programmeradmin1浏览0评论

This is a follow up to my previous question.

I need to place Bootstrap tooltips dynamically. To do so, I need to know the position of the element which triggers the tooltip (works fine) AND the width of the tooltip which is supposed to show up:

Edit for clarification: My tooltips are generated dynamically and then inserted into the content. If they are, for example, too close to the left edge of the screen I need to place them 'right' instead of 'top'. Now I want to get the width of the tooltip to only place it 'right' when it would actually go out of the screen.

$('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';
        }
    }
});

Thanks in advance.

This is a follow up to my previous question.

I need to place Bootstrap tooltips dynamically. To do so, I need to know the position of the element which triggers the tooltip (works fine) AND the width of the tooltip which is supposed to show up:

Edit for clarification: My tooltips are generated dynamically and then inserted into the content. If they are, for example, too close to the left edge of the screen I need to place them 'right' instead of 'top'. Now I want to get the width of the tooltip to only place it 'right' when it would actually go out of the screen.

$('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';
        }
    }
});

Thanks in advance.

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Nov 23, 2012 at 9:57 mazemaze 2,2849 gold badges26 silver badges31 bronze badges 3
  • why you need to place them dinamically while they are just placed automatically by bootstrap? – Filippo oretti Commented Nov 23, 2012 at 10:16
  • if you need to change some tooltip position use css directly , new class and new css rule, maybe i didn't catched what you really need – Filippo oretti Commented Nov 23, 2012 at 10:17
  • My tooltips are generated dynamically and then inserted into the content. If they are, for example, too close to the left edge of the screen I need to place them 'right' instead of 'top'. Now I want to get the width of the tooltip to only place it 'right' when it would actually go out of the screen. – maze Commented Nov 23, 2012 at 10:28
Add a ment  | 

2 Answers 2

Reset to default 6

From a logically point of view it is impossible :) Think about it - the tooltip calls the placement function to get its position, and then it inserts the tip to the page and style it.

However, you can create a dummy-tip with the same features as the soon-to-e tip, and by that get the width. Like this :

$('body').tooltip({
    delay: { show: 300, hide: 0 },
    placement: function(a, element) {

        //title is by tooltip moved to data-original-title
        var title=$(element).attr('data-original-title');

        //create dummy, a div with the same features as the tooltïp
        var dummy=$('<div class="tooltip">'+title+'</div>').appendTo('body');
        var width=$(dummy).width();
        dummy.remove();

        //now you have width 
        //..
        //..

        var position = $(element).position();
        if (position.left > 515) {
            return "left";
        }
        if (position.left < 515) {
            return "right";
        }
        if (position.top < 110){
            return "bottom";
        }
        return "top";
    },
    selector: '[rel=tooltip]:not([disabled])'
});

You could use :

  var dummy = $('<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + title + '</div></div>').appendTo('body');

to get the exact div as tooltip div same height and width.

发布评论

评论列表(0)

  1. 暂无评论