【原创】相对完美的垂直居中popup(modal/dialog),无需监听window.resize事件

<table class="popup" style="position: absolute; background-color: black; border-radius: 10px; top: 50%; left: 50%;">
          <tr>
              <td>
                  <div>
                      <textarea style="margin:10px;  background-color:azure;  height:150px;width:100px;" contenteditable="true"></textarea>
                  </div>                  
              </td>
          </tr>
        </table>
   $(function () {
            var $popup = $('.popup');
            $popup.css({marginLeft: $popup.width() / 2 * -1 + 'px', marginTop: $popup.height() / 2 * -1 + 'px' });
        });

 使用table的目的是,模拟弹窗的高度会根据弹窗的内容高度显示。

 

posted @ 2015-07-31 15:38  二师弟tl  阅读(703)  评论(0编辑  收藏  举报