我嘗試創建一個將其項目拉伸到窗口寬度的菜單。有人能告訴我我做錯了什麼嗎?我知道這個問題已經被問過,但我不知道我的代碼有什麼問題。JQuery伸展按鈕與窗口寬度水平相關
這就是我「米試圖實現的。紅色是窗口
的Javascript
$(function(){
$(window).resize(function (e) {
setTimeout(function() {
resizeButtons();
}, 200);
});
resizeButtons();
});
function resizeButtons() {
var count = $("#menu").length;
var itemwidth = $(window).width/count;
$(".item").css("background", "blue");
$(".item").css("width", itemwidth);
}
CSS
.elementtop {
position: fixed;
top: 0;
}
.elementfooter {
position: fixed;
bottom: 0;
}
.elementright {
right: 0;
}
ul {
min-width:100%;
padding:0;
margin:0;
float:left;
list-style-type:none;
background: #000000;
}
li {
display:inline;
}
a {
overflow: hidden;
text-decoration:none;
color:white;
background-color:purple;
padding:0.2em 0.6em;
border-right:1px solid white;
}
HTML
<div>
<ul id="menu">
<li> <a href="#" class="item">
<span>Text text</span>
</a>
</li>
<li> <a href="#" class="item">
<span>Text2</span>
</a>
</li>
<li> <a href="#" class="item">
<span>Text3</span>
</a>
</li>
</ul>
</div>
在此先感謝。
爲什麼你想用這個jQuery?你可以用純CSS來完成。 – cimmanon
@cimmanon如何用純CSS實現這一目標?我嘗試過,但我無法得到理想的結果。 – Bram