![](/img/trans.png)
[英]CSS Property doesn't apply when screen width changed. Using both css and jQuery
[英]jquery doesn't apply css property
我为正在编写的一个小函数而疯狂。
此功能需要将容器的高度设置为等于其内部存在的最高UL(总计3个UL,并且绝对位置),并且如果第二个或第三个UL低于第一个UL,则将这些UL的高度设置为相等到第一个。
这是功能:
function fixNavHeight() {
var cont = white,
arr = [];
lis = $('nav li');
lis.each(function() {
var li = $(this);
li.mouseenter(function() {
var childrenHeight = $(this).find(secondaryNavigation).height();
cont.css('height', childrenHeight);
setHeight(childrenHeight);
});
});
function setHeight(base) {
arr = [];
cont.find('ul').each(function() {
if($(this).is(':visible')) {
var h = $(this).height();
console.log(h);
if(h <= base) {
$(this).css('height', base);
}
arr.push(h);
var biggest = Math.max.apply(null, arr);
cont.css('height', biggest);
}
});
}
}
一切正常,但如果UL低于base
,则不会设置CSS高度,我不明白为什么。
您能帮我找到问题吗?
我在这里做错了什么?
当使用.css()
应用高度或宽度时,请添加px后缀,如下所示:
cont.css('height', biggest +'px');
对所有类似的呼叫执行相同的操作。
另外,如果您想仅使用数字使用当前语法,则将css属性作为对象传递给.css()
,如下所示:
cont.css({'height': biggest});
尝试在mouseenter
回调中使用console.log
这些项目:
secondaryNavigation
$(this).find(secondaryNavigation)
$(this).find(secondaryNavigation).height()
检查是否已定义secondaryNavigation
。 它来自代码中的任何地方(可能在上面的几行中),并且必须可访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.