移动设备上的 Twitter Bootstrap 模式



Bootstrap 模态在 Android 和 iOS 上无法正常工作。 问题跟踪器确认问题,但不提供有效的解决方案:

2.0 中的模态在移动设备上被破坏了。

2.0 中的模态窗口定位不正确

屏幕变暗,但模态本身在视口中不可见。 可以在页面顶部找到它。 当您在页面上向下滚动时,会出现此问题。

以下是引导响应的相关部分.css:

.modal {
    position:fixed;
    top:50%;
    left:50%;
    z-index:1050;
    max-height:500px;
    overflow:auto;
    width:560px;
    background-color:#fff;
    border:1px solid #999;
    -webkit-border-radius:6px;
    -moz-border-radius:6px;
    border-radius:6px;
    -webkit-box-shadow:0 3px 7px rgba(0, 0, 0, 0.3);
    -moz-box-shadow:0 3px 7px rgba(0, 0, 0, 0.3);
    box-shadow:0 3px 7px rgba(0, 0, 0, 0.3);
    -webkit-background-clip:padding-box;
    -moz-background-clip:padding-box;
    background-clip:padding-box;
    margin:-250px 0 0 -280px;
}

是否有我可以应用的修复程序?

编辑:已经构建了一个非官方的引导模式修改,以解决响应式/移动问题。这也许是解决问题的最简单和最简单的方法。

此后在您之前讨论的一个问题中发现了修复程序

bootstrap-responsive.css

.modal { 
    position: fixed; 
    top: 3%; 
    right: 3%; 
    left: 3%; 
    width: auto; 
    margin: 0; 
}
.modal-body { 
    height: 60%; 
}

并在bootstrap.css

.modal-body { 
    max-height: 350px; 
    padding: 15px; 
    overflow-y: auto; 
    -webkit-overflow-scrolling: touch; 
 }

Gil 的回答很有希望(他链接到的库(,---但就目前而言,在移动设备上向下滚动时它仍然不起作用。

我只用CSS文件末尾的一个CSS片段为自己解决了这个问题:

@media (max-width: 767px) {
  #content .modal.fade.in {
    top: 5%;
  }
}

#content选择器只是一个包装我的 html 的 id,所以我可以覆盖 Bootstrap 的特殊性(设置为你自己的 id 包装你的模态 html(。

缺点:它不是垂直居中移动设备。

好处:它是可见的,在较小的设备上,合理大小的模态将占据屏幕的大部分,因此"非居中"不会那么明显。

为什么有效:

当您使用 Bootstrap 的响应式 CSS 处于低屏幕尺寸时,对于屏幕较小的设备,它会将 .modal.fade.in 的"顶部"设置为"自动"。出于某种原因,移动webkit浏览器似乎很难通过"自动"分配来计算垂直位置。因此,只需将其切换回固定值,它就会很好地工作。

由于模式已设置为 position: absolute ,因此该值相对于视口的高度,而不是文档高度,因此无论页面有多长或滚动到哪里,它都有效。

niftylettuce 在问题 2130 中的解决方案似乎修复了所有移动平台中的模态......

9/1/12 更新:修复已在此处更新:Twitter 引导 jquery 插件

(下面的代码较旧,但仍然有效(

// # Twitter Bootstrap modal responsive fix by @niftylettuce
//  * resolves #407, #1017, #1339, #2130, #3361, #3362, #4283
//   <https://github.com/twitter/bootstrap/issues/2130>
//  * built-in support for fullscreen Bootstrap Image Gallery
//    <https://github.com/blueimp/Bootstrap-Image-Gallery>
// **NOTE:** If you are using .modal-fullscreen, you will need
//  to add the following CSS to `bootstrap-image-gallery.css`:
//
//  @media (max-width: 480px) {
//    .modal-fullscreen {
//      left: 0 !important;
//      right: 0 !important;
//      margin-top: 0 !important;
//      margin-left: 0 !important;
//    }
//  }
//
var adjustModal = function($modal) {
  var top;
  if ($(window).width() <= 480) {
    if ($modal.hasClass('modal-fullscreen')) {
      if ($modal.height() >= $(window).height()) {
        top = $(window).scrollTop();
      } else {
        top = $(window).scrollTop() + ($(window).height() - $modal.height()) / 2;
      }
    } else if ($modal.height() >= $(window).height() - 10) {
      top = $(window).scrollTop() + 10;
    } else {
      top = $(window).scrollTop() + ($(window).height() - $modal.height()) / 2;
    }
  } else {
    top = '50%';
    if ($modal.hasClass('modal-fullscreen')) {
      $modal.stop().animate({
          marginTop  : -($modal.outerHeight() / 2)
        , marginLeft : -($modal.outerWidth() / 2)
        , top        : top
      }, "fast");
      return;
    }
  }
  $modal.stop().animate({ 'top': top }, "fast");
};
var show = function() {
  var $modal = $(this);
  adjustModal($modal);
};
var checkShow = function() {
  $('.modal').each(function() {
    var $modal = $(this);
    if ($modal.css('display') !== 'block') return;
    adjustModal($modal);
  });
};
var modalWindowResize = function() {
  $('.modal').not('.modal-gallery').on('show', show);
  $('.modal-gallery').on('displayed', show);
  checkShow();
};
$(modalWindowResize);
$(window).resize(modalWindowResize);
$(window).scroll(checkShow);

当 Bootstrap 模式对话框打开时,我们使用此代码将其居中。在使用它时,我在iOS上没有遇到任何问题,但我不确定它是否适用于Android。

$('.modal').on('show', function(e) {
    var modal = $(this);
    modal.css('margin-top', (modal.outerHeight() / 2) * -1)
         .css('margin-left', (modal.outerWidth() / 2) * -1);
    return this;
});

诚然,我没有尝试过上面列出的任何解决方案,但是当我在 Bootstrap 3 中尝试 jschr 的 Bootstrap-modal 项目时,我(最终(高兴得跳了起来(链接到顶部答案(。js 给我带来了麻烦,所以我放弃了它(也许我的是一个独特的问题,或者它适用于 Bootstrap 2(,但 CSS 文件本身似乎在 Android 的本机 2.3.4 浏览器中可以解决问题。

就我而言,到目前为止,在使用覆盖以允许现代手机中的预期行为之前,我已经求助于使用(次优(用户代理检测。

例如,如果您希望所有 Android 手机 3.x 及以下版本仅使用全套 hack,您可以在使用 javascript 进行用户代理检测后在正文中添加一个类"oldPhoneModalNeed",然后修改 jschr 的 Bootstrap-modal CSS 属性以始终将 .oldPhoneModalNeed 作为祖先。

您可以在 JavaScript 中全局添加此属性:

if( navigator.userAgent.match(/iPhone|iPad|iPod/i) ) {
    var styleEl = document.createElement('style'), styleSheet;
    document.head.appendChild(styleEl);
    styleSheet = styleEl.sheet;
    styleSheet.insertRule(".modal { position:absolute; bottom:auto; }", 0);
 }

好的,这确实解决了它,我今天尝试了 九月 5-2012 但你必须确保查看演示

niftylettuce 在问题 2130 中的解决方案似乎修复了所有模态 移动平台...

9/1/12 更新:修复程序已在此处更新: Twitter Bootstrap jQuery 插件

这是演示的链接它在这里工作得很好,这是我使用的代码

            title_dialog.modal();
            title_dialog.modalResponsiveFix({})
            title_dialog.touchScroll();

我的解决方案...

Ver en jsfiddle

//Fix modal mobile Boostrap 3
function Show(id){
    //Fix CSS
    $(".modal-footer").css({"padding":"19px 20px 20px","margin-top":"15px","text-align":"right","border-top":"1px solid #e5e5e5"});
    $(".modal-body").css("overflow-y","auto");
    //Fix .modal-body height
    $('#'+id).on('shown.bs.modal',function(){
        $("#"+id+">.modal-dialog>.modal-content>.modal-body").css("height","auto");
        h1=$("#"+id+">.modal-dialog").height();
        h2=$(window).height();
        h3=$("#"+id+">.modal-dialog>.modal-content>.modal-body").height();
        h4=h2-(h1-h3);      
        if($(window).width()>=768){
            if(h1>h2){
                $("#"+id+">.modal-dialog>.modal-content>.modal-body").height(h4);
            }
            $("#"+id+">.modal-dialog").css("margin","30px auto");
            $("#"+id+">.modal-dialog>.modal-content").css("border","1px solid rgba(0,0,0,0.2)");
            $("#"+id+">.modal-dialog>.modal-content").css("border-radius",6);               
            if($("#"+id+">.modal-dialog").height()+30>h2){
                $("#"+id+">.modal-dialog").css("margin-top","0px");
                $("#"+id+">.modal-dialog").css("margin-bottom","0px");
            }
        }
        else{
            //Fix full-screen in mobiles
            $("#"+id+">.modal-dialog>.modal-content>.modal-body").height(h4);
            $("#"+id+">.modal-dialog").css("margin",0);
            $("#"+id+">.modal-dialog>.modal-content").css("border",0);
            $("#"+id+">.modal-dialog>.modal-content").css("border-radius",0);   
        }
        //Aply changes on screen resize (example: mobile orientation)
        window.onresize=function(){
            $("#"+id+">.modal-dialog>.modal-content>.modal-body").css("height","auto");
            h1=$("#"+id+">.modal-dialog").height();
            h2=$(window).height();
            h3=$("#"+id+">.modal-dialog>.modal-content>.modal-body").height();
            h4=h2-(h1-h3);
            if($(window).width()>=768){
                if(h1>h2){
                    $("#"+id+">.modal-dialog>.modal-content>.modal-body").height(h4);
                }
                $("#"+id+">.modal-dialog").css("margin","30px auto");
                $("#"+id+">.modal-dialog>.modal-content").css("border","1px solid rgba(0,0,0,0.2)");
                $("#"+id+">.modal-dialog>.modal-content").css("border-radius",6);               
                if($("#"+id+">.modal-dialog").height()+30>h2){
                    $("#"+id+">.modal-dialog").css("margin-top","0px");
                    $("#"+id+">.modal-dialog").css("margin-bottom","0px");
                }
            }
            else{
                //Fix full-screen in mobiles
                $("#"+id+">.modal-dialog>.modal-content>.modal-body").height(h4);
                $("#"+id+">.modal-dialog").css("margin",0);
                $("#"+id+">.modal-dialog>.modal-content").css("border",0);
                $("#"+id+">.modal-dialog>.modal-content").css("border-radius",0);   
            }
        };
    });  
    //Free event listener
    $('#'+id).on('hide.bs.modal',function(){
        window.onresize=function(){};
    });  
    //Mobile haven't scrollbar, so this is touch event scrollbar implementation
    var y1=0;
    var y2=0;
    var div=$("#"+id+">.modal-dialog>.modal-content>.modal-body")[0];
    div.addEventListener("touchstart",function(event){
        y1=event.touches[0].clientY;
    });
    div.addEventListener("touchmove",function(event){
        event.preventDefault();
        y2=event.touches[0].clientY;
        var limite=div.scrollHeight-div.clientHeight;
        var diff=div.scrollTop+y1-y2;
        if(diff<0)diff=0;
        if(diff>limite)diff=limite;
        div.scrollTop=diff;
        y1=y2;
    });
    //Fix position modal, scroll to top.    
    $('html, body').scrollTop(0);
    //Show
    $("#"+id).modal('show');
}

找到了一个非常黑客的解决方案,但它有效。我在用于打开模态的链接中添加了一个类(使用数据目标(,然后使用 Jquery,向该类添加一个 click 事件,该事件获取数据目标,找到它应该打开的模态,然后通过 Javascript 打开它。对我来说效果很好。我还在我的设备上添加了移动检查,以便它仅在移动设备上运行,但这不是必需的。

$('.forceOpen').click(function() {
  var id = $(this).attr('data-target');
  $('.modal').modal('hide');
  $(id).modal('show');
});

主要是Nexus 7模态问题,模态正在下降屏幕

  .modal:before {
    content: '';
    display: inline-block;
    height: 50%; (the value was 100% for center the modal)
    vertical-align: middle;
    margin-right: -4px;
  }
对我来说

$('[data-toggle="modal"]').click(function(){});工作正常。

尽管这个问题已经很老了,但有些人在寻找改善手机上模态用户体验的解决方案时仍然可能会偶然发现它。

我做了一个库来改善Bootrsrap模态在手机上的行为。

引导程序 3:https://github.com/keaukraine/bootstrap-fs-modal

引导程序 4:https://github.com/keaukraine/bootstrap4-fs-modal

最新更新