Twitter Bootstrap modal on mobile devices

前端 未结 12 1947
忘了有多久
忘了有多久 2020-12-02 04:20

Bootstrap modals don\'t work correctly on Android and iOS. The issue tracker acknowledges the problem but does not offer a working solution:

Modals in 2.0 are broke

相关标签:
12条回答
  • 2020-12-02 05:00

    My solution...

    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');
    }
    
    0 讨论(0)
  • 2020-12-02 05:03

    EDIT: An unofficial Bootstrap Modal modification has been built to address responsive/mobile issues. This is perhaps the simplest and easiest way to remedy the problem.

    There has since been a fix found in one of the issues you discussed earlier

    in bootstrap-responsive.css

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

    and in bootstrap.css

    .modal-body { 
        max-height: 350px; 
        padding: 15px; 
        overflow-y: auto; 
        -webkit-overflow-scrolling: touch; 
     }
    
    0 讨论(0)
  • 2020-12-02 05:08

    OK this does fix it I tried it today Sept 5-2012 but you have to be sure to check out the demo

    The solution by niftylettuce in issue 2130 seems to fix modals in all mobile platforms...

    9/1/12 UPDATE: The fix has been updated here: twitter bootstrap jquery plugins

    here is the link to the Demo It works great heres the code I used

                title_dialog.modal();
                title_dialog.modalResponsiveFix({})
                title_dialog.touchScroll();
    
    0 讨论(0)
  • 2020-12-02 05:09

    The solution by niftylettuce in issue 2130 seems to fix modals in all mobile platforms...

    9/1/12 UPDATE: The fix has been updated here: twitter bootstrap jquery plugins

    (the code below is older but still works)

    // # 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);
    
    0 讨论(0)
  • 2020-12-02 05:09

    Albeit this question is quite old, some people still may stumble upon it when looking for solution to improve UX of modals on mobile phones.

    I've made a lib to improve Bootrsrap modals' behavior on phones.

    Bootstrap 3: https://github.com/keaukraine/bootstrap-fs-modal

    Bootstrap 4: https://github.com/keaukraine/bootstrap4-fs-modal

    0 讨论(0)
  • 2020-12-02 05:12

    We use this code to center the Bootstrap modal dialogs when they open. I haven't had any issue with them on iOS while using this but I'm not sure if it will work for 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;
    });
    
    0 讨论(0)
提交回复
热议问题