zepto扩展outerWidth与outerHeight方法

扩展outerWidth,outerHeight方法

(function($){
    "use strict";

    ['width','height'].forEach(function(dimension){

        var Dimension = dimension.replace(/./,function(m){
            return m[0].toUpperCase();
        });

        //outerWidth or outerHeight

        $.fn['outer' + Dimension] = function(margin) {
            var elem = this;

            if(elem) {


                // elem.width(); or  elem.height();
                var size = elem[dimension]();

                var sides = {
                    'width' : ['left', 'right'],
                    'height' : ['top', 'bottom']
                };

                sides[dimension].forEach(function(side){
                    if(margin) {
                        size += parseInt(elem.css('margin-'+side),10);
                    }
                });

                return size;
            } else {
                return null;
            }
        }


    });
})($);

 

posted @ 2016-09-25 15:12  熊二(李明)  阅读(2912)  评论(0编辑  收藏  举报