jQuery 中的等高列
你好,我正在寻找一个基于 jQuery 的等高列。我知道有很多这样的东西,但我的要求有点不同。我想在超级菜单中使用它们,其中有大约 4-5 个下拉菜单,每个下拉菜单有 3-4 列。
我希望所有这些 3-4 列具有相同的高度,但不是在所有下拉菜单中,因为根据该部分的内容,其他下拉菜单中的列高度会有所不同。
我在 MooTools 中找到了一个非常适合我的要求的解决方案。下面的 MooTools 代码使特定 div 中的所有列等于其父 div 的高度
MooTools 代码:
var Equalizer = new Class({
initialize: function(elements) {
this.elements = $$(elements);
},
equalize: function(hw) {
if(!hw) { hw = 'height'; }
var max = 0,
prop = (typeof document.body.style.maxHeight != 'undefined' ? 'min-' : '') + hw; //ie6 ftl
offset = 'offset' + hw.capitalize();
this.elements.each(function(element,i) {
var calc = element[offset];
if(calc > max) { max = calc; }
},this);
this.elements.each(function(element,i) {
element.setStyle(prop,max - (element[offset] - element.getStyle(hw).toInt()));
});
return max;
}
});
用法:
var columnizer = new Equalizer('.sizeMe').equalize('height'); //call .equalize() as often as you want!
有人可以帮我在 jQuery 中转换此代码吗?实际上,我的整个模板都是基于 jQuery 的,只是为了这个等高函数,我不想加载另一个 JavaScript 库。
请帮忙!
Hi I am looking for a jQuery based equal height columns. I know there are a lot of them floating around but my requirement is a bit different. I want to use these in a Mega Menu where thee are about 4-5 drop-downs and each drop-down has 3-4 columns.
I want all these 3-4 columns of equal height but not in all drop-downs, because the columns height will be different in the other drop-down depending on the content of that section.
I found a solution in MooTools which works perfect for my requirement. The MooTools code below makes all the columns in a particular div equal to it's parent div's height
The MooTools Code :
var Equalizer = new Class({
initialize: function(elements) {
this.elements = $(elements);
},
equalize: function(hw) {
if(!hw) { hw = 'height'; }
var max = 0,
prop = (typeof document.body.style.maxHeight != 'undefined' ? 'min-' : '') + hw; //ie6 ftl
offset = 'offset' + hw.capitalize();
this.elements.each(function(element,i) {
var calc = element[offset];
if(calc > max) { max = calc; }
},this);
this.elements.each(function(element,i) {
element.setStyle(prop,max - (element[offset] - element.getStyle(hw).toInt()));
});
return max;
}
});
Usage :
var columnizer = new Equalizer('.sizeMe').equalize('height'); //call .equalize() as often as you want!
Can somebody help me converting this code in jQuery please. Actually my entire template is jQuery based and just for this equal-height function I do not want to load another JavaScript library.
Kindly Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我认为这可能有用,所以将其制作成 jQuery 插件供您使用。
演示: http://jsfiddle.net/AXqBb/
equalizer.js:
调用像这样:
我使代码尽可能与您发布的代码相似,这样您就可以看到更改,因此对任何不好的风格表示歉意 - 希望它可归因于原作者。 ;-)
注意。我在这段代码中向 String 添加了基本的首字大写功能;如果已经定义,则需要将其删除。
Right, thought that might be useful so made it in to a jQuery plugin for you.
Demo: http://jsfiddle.net/AXqBb/
equalizer.js:
Called like this:
I have kept the code as similar to what you posted as possible, so you can see the changes, so apologise for any bad style - hopefully it is attributable towards the original author. ;-)
NB. I added a basic first-word capitalisation function to String within this code; if already defined that would need to be removed.