javascript - 使用 Bootstrap 弹出窗口作为模式

标签 javascript html twitter-bootstrap fixed popover

我使用了搜索,有很多关于 Bootstrap 弹出窗口的问题,但我有一个特殊的问题。

很多人问如何修复弹出窗口,但我希望我的弹出窗口具有与 Bootstrap “模态”相同的功能(从顶部出现的东西,让屏幕变暗,你必须退出它才能返回网站)。必须单击我的弹出窗口,然后它就会出现并一直保留,直到您单击其他任何地方(如果您明白我的意思的话)。

最佳答案

正如我的评论中所述:

Why don't you use the modal in the first place? I guess customizing that to what you need could be easier then hacking popovers to behave like modals

this issue on Github @Fat建议改用焦点触发方法。 然而,我刚刚意识到我曾经在一个项目中遇到过同样的问题,我通过 Hook 可用事件并执行一些小技巧解决了它:

$( '.popover-element' )
    .on( 'show.bs.popover', function ( e ) {
        $( 'body' ).one( 'click', function ( e ) {
            if ( !$( e.target ).parents( '.popover-element' ).length ) {
                // After the popover has been opened we catched a
                // click event that bubbled up to the body element.
                // Since the .popover-element is not a parent of the
                // element (.length === 0) that originated this event
                // we can assume that the user clicked outside of it 
                // and can thus close the popover.
                $( '.popover-element ).popover( 'hide' );
            }
        } );
    } );

另请参阅event.targetpopover documentation (尤其是方法和事件)。

<小时/>

这是一个应该与您的源兼容的版本:

$( '[rel=popover]' )
    .popover( {
        trigger: 'hover',
        html:    true,
        delay:   500      
    } )
    .on( 'show.bs.popover', function ( e ) {
        var $popover = $( this );
        $( 'body' ).one( 'click', function ( e ) {
            if ( !$( e.target ).parents().filter( $popover ).length ) {
                $popover.popover( 'hide' );
            }
        } );
    } );

关于javascript - 使用 Bootstrap 弹出窗口作为模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24375161/

相关文章:

html - 如何使用 CSS 隐藏文本?

css - 我如何找出为什么我的移动版本没有缩小以适合屏幕?

javascript - 在设计模式下禁用在 mozilla 中调整表格大小

javascript - 在 tr 内打破 5 组 10 td

javascript - 将 javascript 数组插入到另一个数组中

css - img 标签 html 错误中的 Alt 属性

javascript - 如果满足条件,使用 JQuery 添加指向元素的链接

twitter-bootstrap - 导航栏折叠在移动版本中不起作用?

jquery - ASP.net 中的 MasterPage 和 Bootstrap 更改事件页面

javascript - 使用纯 CSS 在响应式容器中垂直居中按钮?