// javascript document /** * 给统一的样式设置高度 * @author lijingui * @param options(参数组) */ function liheight(options) { var self = this; self.options = $.extend({}, liheightdefaults, options || {}); self._init(); } var cfg; $.extend(liheight.prototype, { // 页面初始化 _init : function() { var self = this, cfg = self.options; if (cfg.targetcls == null || $(cfg.targetcls + "")[0] === undefined) { if (window.console) { //console.log("targetcls不为空!"); } return; } if (!cfg.fixedmaxheight && cfg.fixedheight == null) { //console.log("targetcls默认不操作,随文本自动变化高度"); return; } self._componentsetheight(cfg.fixedmaxheight, cfg.targetcls, cfg.fixedheight); }, /** * 给组件设置高度 * @param fixedmaxheight:是否按照'目标选择器'最高度的高度来设置 * @param targetclass:目标选择器值 * @param fixedheight:固定高度(默认没有固定高度,按照内容自己变化) */ _componentsetheight : function(fixedmaxheight, targetclass, fixedheight) { var curheight = this._componentgetheight(fixedmaxheight, targetclass, fixedheight); $(targetclass).each(function() { $(this).css("height", curheight); }); }, /** * 得到要设置的高度 * @param fixedmaxheight:是否按照'目标选择器'最高度的高度来设置 * @param targetclass:目标选择器值 * @param fixedheight:固定高度(默认没有固定高度,按照内容自己变化) * @returns 要设置的高度值 */ _componentgetheight : function(fixedmaxheight, targetclass, fixedheight) { var result = fixedheight; // 所有的设置统一固定高度(死值) if (!fixedmaxheight && fixedheight != null) { //console.log("所有的设置统一固定高度(死值):" + result); return result; } // 得到目标选择器里面最大的高度 result = 0; //因为放到标签时 初始化也调用了此方法 但是不显示的元素获取的高度为0 //所以再此调用此方法的时候就一直为0了 这里要设置为auto一下 才能获取正确的高度 $(targetclass).css('height', 'auto'); $(targetclass).each(function() { var curheight = $(this).height(); if (curheight > result) { result = curheight; } }); //console.log("得到所有对象里面最高的值:" + result); return result; } }); var liheightdefaults = { 'targetcls' : null, // 要处理的容器 'fixedmaxheight' : true, // 是否按照最高度的高度来设置,默认是true 'fixedheight' : null // 固定高度(默认没有固定高度,按照内容自己变化) }; // 调用说明 // 不设置值,默认随文本变化调用方法 //new liheight({ "targetcls" : '.liclass', "fixedmaxheight":false, "fixedheight": null }); // 设置固定值 //new liheight({ "targetcls" : '.liclass', "fixedmaxheight":false, "fixedheight": 20 }); // 不设置值,默认设置目标选择器里面的最大的高度 //new liheight({ "targetcls" : '.liclass', "fixedmaxheight":true, "fixedheight": null });