如何用插件让盒子垂直水平居中

《js代码插件例子》

;(function($){
    //对象级别
    $.fn.center = function(options){
        var defaults = {
            "position":"absolute",
            "background":"red",
            "width":100,
            "height":100
        }
        
        var options = $.extend(defaults,options);
        this.each(function(){
            //功能实现逻辑
            var _this = $(this)
            var _vH = ($(window).height()-options.height)/2
            var _vW = ($(window).width()-options.width)/2
            _this.css({
                "background":options.background,
                "position":options.position,
                "width":options.width,
                "height":options.height,
                "left":_vW,
                "top":_vH
            })
            
        })
        return this        
    }
    
    
    
})(jQuery)

《html代码例子》

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script type="text/javascript" src="js/jquery-1.11.0.js" ></script>
        <script type="text/javascript" src="js/center-1.1.js" ></script>
        <style>
            *{margin: 0;}
        </style>
    </head>
    <body>
        <div id="box">
            
        </div>
        <script>
            $("#box").center()
        </script>
        
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_42161401/article/details/82496291
今日推荐