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, }; })();