Dynamically position Bootstrap tooltip (for dynamically generated elements)

后端 未结 6 1953
粉色の甜心
粉色の甜心 2020-12-13 19:43

I am using the Tooltips provided by Twitter Bootstrap (http://twitter.github.com/bootstrap/javascript.html#tooltips).

I have some dynamically inserted markup in my D

相关标签:
6条回答
  • 2020-12-13 20:16

    This is shorthand that i use to determine window width is smaller 768 or not, then change tooltip placement from left to top:

    placement: function(){return $(window).width()>768 ? "auto left":"auto top";}
    
    0 讨论(0)
  • 2020-12-13 20:26

    $(element).position() will not work properly if the option container is set to the tooltip.

    In my case, I use container : 'body' and have to use $(element).offset() instead.

    0 讨论(0)
  • 2020-12-13 20:33

    this one worked for me

    $("[rel=tooltip]").popover({
                placement: function(a, element) {
                   var position = $(element).parent().position();
                   console.log($(element).parent().parent().is('th:first-child'));
    
                    if ($(element).parent().parent().is('th:last-child')) {
                        return "left";
                    }
                    if ($(element).parent().parent().is('th:first-child')) {
                        return "right";
                    }
                    return "bottom";
                },
    
            });
    
    0 讨论(0)
  • 2020-12-13 20:34

    Bootstrap calls the placement function with params that includes the element

    this.options.placement.call(this, $tip[0], this.$element[0])
    

    so in your case, do this :

    $('body').tooltip({
        delay: { show: 300, hide: 0 },
        placement: function(tip, element) { //$this is implicit
            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])'
    });
    
    0 讨论(0)
  • 2020-12-13 20:35

    The placement option in docs allows for function . It isn't documented ( that I could find) what arguments are in the function. This is easy to determine however by logging arguments to console.

    Here's what you can use:

    $('body').tooltip({
    
       placement: function(tip, el){
         var position = $(el).position();
          /* code to return value*/
    
      }
    
    /* other options*/
    })
    
    0 讨论(0)
  • 2020-12-13 20:37

    This is how I place my tooltip in Bootstrap 2.3.2

    placement: function (tooltip, trigger) {
        window.setTimeout(function () {
            $(tooltip)
                .addClass('top')
                .css({top: -24, left: 138.5})
                .find('.tooltip-arrow').css({left: '64%'});
    
            $(tooltip).addClass('in');
        }, 0);
    }
    

    Basically what I'm saying here is that my tooltip will be positioned on the top with some custom offset, also the little triangle arrow on the bottom will be slightly to the right.

    At the end I'm adding the in class which shows the tooltip.

    Also note that the code is wrapped in setTimeout 0 function.

    0 讨论(0)
提交回复
热议问题