jquery封装(-)

来源:互联网 发布:nginx后端服务器监控 编辑:程序博客网 时间:2024/06/07 22:37

页面弹窗的写法:

<div class="pop-bg">

           <div class="pop"></div>

</div>

.pop-bg{width: 100%;  height:100%;  overflow-y:scroll;  position:fixed;  top:0;  left:0;  background:rgba(66,66,66,.5);}

.pop{width: 660px;position:relative;background:#fff;margin:auto;}

因为每个div.pop的高度可能不同,所以需要动态设置div.pop在可视窗口内上下居中,此时需封装一个函数:

<script type="text/javascript">

      // 扩展jQuery,显示窗口并窗口居中


            $.fn.showCenter = function(params){
                 var thisWOb = $(this).parent();
                 if (typeof(params) != 'undefined' &&
                    typeof(params.isShowParent) &&
                           params.isShowParent == false) {
                        // 为false的时候,选择本身
                        thisWOb = $(this);
                 }

                //params,所传递的参数

               //isShowParent,所传递的参数的key

              //false,所传递的参数的key值


                 // 显示出来了,才能准确设置div.pop的上下位置
                 thisWOb.fadeIn();
                // div.pop的高度
                 var selfHeight = $(this).outerHeight();
                 // 得到窗口的高度
                 var windowHeight = $(window).height();
                 var c = (parseFloat(windowHeight) - parseFloat(selfHeight)) / 2;

                 if (c >= 0) {
                      $(this).css('top', c);
                 } else {
                       $(this).css('top', 0);

                 }
            }

</script>


调用方法具体为:$("#transaction-pop-bg  .pop").showCenter();