![](/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.