SoFunction
Updated on 2025-03-01

bootstrap modal box vertical centering effect

The effect of this example is actually a click pop-up effect for your reference. The specific content is as follows

First, add jquery code

 //The modal box is vertically centered function centerModals() {
  $('.modal').each(
    function(i) {
     var $clone = $(this).clone().css('display', 'block')
       .appendTo('body');
     var top = (($() - $(
       '.modal-content').height()) / 2);
     top = top > 0 ? top : 0;
     $();
     $(this).find('.modal-content').css("margin-top",
       top - 50);
    });
 }
 $('.modal').on('', centerModals);
 $(window).on('resize', centerModals);

html code

<!--Login pop-up window-->
 <form action="__URL__/doLogin" method="post" class="form-horizontal">

  <!--Modal frame name-->
  <div class="modal"  tabindex="-1">
   <!--Window name-->
   <div class="modal-dialog modal-sm">
    <!--Content name-->
    <div class="modal-content">
     <div class="modal-header">
      <button class="close" data-dismiss="modal">
       <span>×</span>
      </button>
      <h4 class="modal-title">User login</h4>
     </div>

     <div class="modal-body">
      <div class="form-group has-success has-feedback">

       <label for="username" class="col-sm-3 control-label">account</label>
       <div class="col-sm-9">
        <span class="glyphicon glyphicon-user form-control-feedback"
         aria-hidden="true"></span> <span 
         class="sr-only">(success)</span> <input type="text"
          name="username" class="form-control"
         placeholder="Please enter your username" title="Can contain Chinese numbers and common characters"
         onkeydown="if(==32||==13){return false;}">
        <!-- Disable spaces and enter -->

       </div>
      </div>


      <div class="form-group has-success has-feedback">
       <label for="password" class="col-sm-3 control-label">password</label>
       <div class="col-sm-9">
        <span class="glyphicon glyphicon-lock form-control-feedback"
         aria-hidden="true"></span> <span 
         class="sr-only">(success)</span> <input type="password"
          name="password" class="form-control"
         maxlength="12" placeholder="Please enter your password" oncopy="return false"
         oncut="return false" onpaste="return false" title=""
         onkeydown="if(==32||==13){return false;}">

       </div>
      </div>
     </div>
     <div class="modal-footer">
      <div class="form-group">
       <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-success" >Log in now
         »</button>
       </div>
      </div>
     </div>
    </div>
   </div>
  </div>
 </form>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.