Twitter Bootstrap modal on mobile devices

前端 未结 12 2075
忘了有多久
忘了有多久 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: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
    //   
    //  * built-in support for fullscreen 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);
    

提交回复
热议问题