使用 twitter 引导程序创建工具提示/弹出框后的回调函数?显示如何扩展 javascript。使用它根据显示前计算的位置添加展示位置:
var tmp = $.fn.popover.Constructor.prototype.show;
$.fn.popover.Constructor.prototype.show = function () {
/* random placement add your calcuations based $(this).data('isotope-item-position'); on here: */
var items = Array('top','bottom','left','right');
this.options.placement = items[Math.floor(Math.random()*items.length)];
// from: https://stackoverflow.com/questions/5915096/get-random-item-from-array-with-jquery
tmp.call(this);
}
见:http ://bootply.com/66412
更新
在您的扩展显示函数this.$element中是对触发弹出窗口的元素的引用。您可以使用它来获取其位置(http://api.jquery.com/position/)。
var tmp = $.fn.popover.Constructor.prototype.show;
$.fn.popover.Constructor.prototype.show = function () {
var position = this.$element.position();
if(position.top<50){this.options.placement = 'bottom'}
else if(position.top>150){this.options.placement = 'top'}
else if(position.left<200){this.options.placement = 'right'}
else {this.options.placement = 'left'}
tmp.call(this);
}
见:http ://bootply.com/66412 (也更新了)
https://stackoverflow.com/a/12656175/1596547将通过将放置作为函数调用来提供更好的解决方案:
var options = {
placement: function (context, source) {
var position = $(source).position();
if (position.top < 100){
return "bottom";
}
if (position.top > 800){
return "top";
}
if (position.left > 484) {
return "left";
}
//if (position.left < 485) {
else
{
return "right";
}
}
, trigger: "click",
title: 'popover',
content: 'content of a popover'
};
$('.element').popover(options);
}
popover()不是“现场”功能。当新元素插入 DOM 时,您将不得不再次调用 popover。请参阅http://www.infinite-scroll.com/ “所有选项”无限滚动在新内容成功加载时有一个可选的回调。使用它再次调用 popover()。请参阅:http ://bootply.com/66524(bootply 不加载第二页)。
完整来源:
function setpopover()
{
var options = {
placement: function (context, source) {
var position = $(source).position();
if (position.top < 100){
return "bottom";
}
if (position.top > 800){
return "top";
}
if (position.left > 484) {
return "left";
}
//if (position.left < 485) {
else
{
return "right";
}
}
, trigger: "click",
title: 'popover',
content: 'content of a popover'
};
$('.element').popover(options);
}
$(function(){
var $container = $('#container');
$container.isotope({
itemSelector : '.element',
itemPositionDataEnabled: true
});
$container.infinitescroll({
navSelector : '#page_nav', // selector for the paged navigation
nextSelector : '#page_nav a', // selector for the NEXT link (to page 2)
itemSelector : '.element', // selector for all items you'll retrieve
loading: {
finishedMsg: 'No more pages to load.',
img: 'http://i.imgur.com/qkKy8.gif'
}
},
// call Isotope as a callback
function( newElements ) {
$container.isotope( 'appended', $( newElements ) );
setpopover();
}
);
setpopover()
});
注意:元素的位置由 jQuery 的位置 () 给出。这将为您提供元素相对于偏移父级的当前位置。将此与 .offset() 进行对比,后者检索相对于文档的当前位置。见:http ://api.jquery.com/position/ 。isotope-item-position 将给出相同的位置。要设置相对于可见窗口的位置,请参见:
等等