有谁知道 twitter bootstrap 的 popover 组件的扩展,它动态更改放置选项以确保 popover 显示在屏幕上?
问问题
11797 次
2 回答
24
位置可以是函数而不是字符串。一个由fat编写的自动放置示例,然后由wleeper移植到最新版本的引导程序,这里是项目的 github 问题之一:https ://github.com/twitter/bootstrap/issues/345
这是将 CoffeeScript 编译为 JavaScript 的结果:
$("a[rel=popover]").popover({
placement: function(tip, element) {
var $element, above, actualHeight, actualWidth, below, boundBottom, boundLeft, boundRight, boundTop, elementAbove, elementBelow, elementLeft, elementRight, isWithinBounds, left, pos, right;
isWithinBounds = function(elementPosition) {
return boundTop < elementPosition.top && boundLeft < elementPosition.left && boundRight > (elementPosition.left + actualWidth) && boundBottom > (elementPosition.top + actualHeight);
};
$element = $(element);
pos = $.extend({}, $element.offset(), {
width: element.offsetWidth,
height: element.offsetHeight
});
actualWidth = 283;
actualHeight = 117;
boundTop = $(document).scrollTop();
boundLeft = $(document).scrollLeft();
boundRight = boundLeft + $(window).width();
boundBottom = boundTop + $(window).height();
elementAbove = {
top: pos.top - actualHeight,
left: pos.left + pos.width / 2 - actualWidth / 2
};
elementBelow = {
top: pos.top + pos.height,
left: pos.left + pos.width / 2 - actualWidth / 2
};
elementLeft = {
top: pos.top + pos.height / 2 - actualHeight / 2,
left: pos.left - actualWidth
};
elementRight = {
top: pos.top + pos.height / 2 - actualHeight / 2,
left: pos.left + pos.width
};
above = isWithinBounds(elementAbove);
below = isWithinBounds(elementBelow);
left = isWithinBounds(elementLeft);
right = isWithinBounds(elementRight);
if (above) {
return "top";
} else {
if (below) {
return "bottom";
} else {
if (left) {
return "left";
} else {
if (right) {
return "right";
} else {
return "right";
}
}
}
}
}
});
除了一种情况外,它对我来说效果很好:如果该项目位于右上角,则作为选项之一的弹出框没有出现的好位置,并且它部分显示在屏幕外。
于 2012-06-07T17:48:45.427 回答
13
对于那些对采用默认放置(使用data-placement
元素上的属性)的解决方案感兴趣的人,我已经改编了 Cymen 的出色答案。
我还确保没有不必要地计算边界,因此它的性能应该稍高一些。
$('[data-toggle="popover"]').each(function() {
var trigger = $(this);
trigger.popover({
animation: true,
delay: { show: 0, hide: 0 },
html: true,
trigger: 'hover focus',
placement: getPlacementFunction(trigger.attr("data-placement"), 283, 117)
});
});
var getPlacementFunction = function (defaultPosition, width, height) {
return function (tip, element) {
var position, top, bottom, left, right;
var $element = $(element);
var boundTop = $(document).scrollTop();
var boundLeft = $(document).scrollLeft();
var boundRight = boundLeft + $(window).width();
var boundBottom = boundTop + $(window).height();
var pos = $.extend({}, $element.offset(), {
width: element.offsetWidth,
height: element.offsetHeight
});
var isWithinBounds = function (elPos) {
return boundTop < elPos.top && boundLeft < elPos.left && boundRight > (elPos.left + width) && boundBottom > (elPos.top + height);
};
var testTop = function () {
if (top === false) return false;
top = isWithinBounds({
top: pos.top - height,
left: pos.left + pos.width / 2 - width / 2
});
return top ? "top" : false;
};
var testBottom = function () {
if (bottom === false) return false;
bottom = isWithinBounds({
top: pos.top + pos.height,
left: pos.left + pos.width / 2 - width / 2
});
return bottom ? "bottom" : false;
};
var testLeft = function () {
if (left === false) return false;
left = isWithinBounds({
top: pos.top + pos.height / 2 - height / 2,
left: pos.left - width
});
return left ? "left" : false;
};
var testRight = function () {
if (right === false) return false;
right = isWithinBounds({
top: pos.top + pos.height / 2 - height / 2,
left: pos.left + pos.width
});
return right ? "right" : false;
};
switch (defaultPosition) {
case "top":
if (position = testTop()) return position;
case "bottom":
if (position = testBottom()) return position;
case "left":
if (position = testLeft()) return position;
case "right":
if (position = testRight()) return position;
default:
if (position = testTop()) return position;
if (position = testBottom()) return position;
if (position = testLeft()) return position;
if (position = testRight()) return position;
return defaultPosition;
}
}
};
于 2013-09-06T13:49:23.157 回答