Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  169] [ 5]  / answers: 1 / hits: 43281  / 12 Years ago, thu, november 22, 2012, 12:00:00

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 DOM which needs to trigger the tooltips. That's why I trigger tooltips in the following way (https://github.com/twitter/bootstrap/issues/4215):



$('body').tooltip({
delay: { show: 300, hide: 0 },
selector: '[rel=tooltip]:not([disabled])'
});


To avoid tooltips being placed too close to the edge of the screen, I need to be able to set their position dynamically based on where the element which triggers the tooltip is positioned. I thought of doing it the following way:



   $('body').tooltip({
delay: { show: 300, hide: 0 },
// here comes the problem...
placement: positionTooltip(this),
selector: '[rel=tooltip]:not([disabled])'
});



function positionTooltip(currentElement) {
var position = $(currentElement).position();

if (position.left > 515) {
return left;
}

if (position.left < 515) {
return right;
}

if (position.top < 110){
return bottom;
}

return top;
}


How can I pass currentElement correctly to the positionTooltip function in order to return the appropriate placement value for each tooltip?



Thanks in advance


More From » jquery

 Answers
3

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

[#81855] Tuesday, November 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dahlias

Total Points: 730
Total Questions: 104
Total Answers: 101

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;