I have scoured the Internet far and wide and while I found this stackoverflow post insightful Is it possible to change the position of Bootstrap popovers based on screen width?, it still didn't answer my question, likely due to my trouble understanding position/offset.

这就是我想要做的.我希望Twitter Bootstap弹出窗口的位置是正确的,除非热点弹出窗口将位于视口之外,然后我希望它位于左侧.我正在制作一款有热点的iPad web应用,当你按下热点时,信息会出现,但我不想让它在水平滚动被锁定时出现在视口之外.

I'm thinking it'd go something like this:

$('.infopoint').popover({
        trigger:'hover',
        animation: false,
        placement: wheretoplace 
    });
    function wheretoplace(){
        var myLeft = $(this).offset.left;

        if (myLeft<500) return 'left';
        return 'right';
    }

Thoughts? Thanks!

推荐答案

我刚刚注意到,placement选项可以是stringfunction returning a string,每次您单击可弹出的链接时都会进行计算.

This makes it real easy to replicate what you did without the initial $.each function:

var options = {
    placement: function (context, source) {
        var position = $(source).position();

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

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

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

        return "top";
    }
    , trigger: "click"
};
$(".infopoint").popover(options);

Jquery相关问答推荐

在正则表达式字符括号中包含连字符?

jquery如何判断ajax调用的响应类型

$.post 和 $.ajax 之间的区别?

$(document).scrollTop() 总是返回 0

JavaScript 或 jQuery 字符串以实用函数结尾

Facebook 风格的 JQuery 自动完成插件

使用 jQuery 发送 JSON 数据

子元素点击事件触发父点击事件

在javascript中检测按键的最简单方法

如何在 Angular 4 中使用 jQuery 插件?

jQuery:获取父母,父母ID?

jQuery UI 工具提示不支持 html 内容

使用 jquery 在 radio 上单击或更改事件

如何使用 jQuery 或纯 JS 重置所有复选框?

如何使用 javascript 展开和折叠

如何使用 jQuery Validation Plugin 以编程方式判断表单是否有效

由于滚动,响应表内的 bootstrap 按钮下拉菜单不可见

jQuery - 添加 ID 而不是类

如何插入元素作为第一个子元素?

jQuery:通过 .attr() 添加两个属性;方法