js自定义函数:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jacke121/article/details/83311358

window.utils中定义了三个子函数:

addStyleAttribute: addStyleAttribute, getElInfo: getElInfo, resizeNameEl: resizeNameEl,

语法:

window.utils = (function() {

function  a(){

}

return {

a:a

}

})();

结尾的})();不能少

window.utils = (function() {
    function addStyleAttribute($element, styleAttribute) {
        var myStyleString = '';
        for (var key in styleAttribute) {
            myStyleString += key + ': ' + styleAttribute[key] + ';';
        }
        $element.attr('style', $element.attr('style') + '; ' + myStyleString);
    }

    function getElInfo(el, key) {
        var $el = $(el).clone().css({
            'display': 'inline',
            'visibility': 'hidden',
        });
        $('body').append($el);
        if ($el.outerWidth()==null)
            alert("outerWidth null")
        var info = {
            outerWidth: $el.outerWidth(),
            outerHeight: $el.outerHeight(),
        };
        $el.remove();
        return info[key] || info;
    };

    function resizeNameEl(card) {
        if (!card) return;
        // update name font position related to name length
        addStyleAttribute(card.find('.name'), {
            'margin-left': '3px !important',
            'width': '90% !important',
            'word-break': 'break-all !important',
            'font-size': '2.3vw !important',
            'line-height': '1.5 !important',
        });
        var nameElRealWidth = getElInfo(card.find('.name'), 'outerWidth');
        if (nameElRealWidth > card.outerWidth() - 30) {
            // card.find('.name').css({
            //     'top': '65%'
            // });
            var nameElRealWidth = getElInfo(card.find('.name'), 'outerWidth');
            if (nameElRealWidth > card.outerWidth() - 30) {
                card.find('.name').css({
                    'top': '65%'
                });
            }
        }
    }
        return {
            addStyleAttribute: addStyleAttribute,
            getElInfo: getElInfo,
            resizeNameEl: resizeNameEl,
        };

})();

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/83311358