动态生成且兼容浏览器的等宽菜单项
我正在创建一个导航菜单,其中每个菜单项的宽度都需要相等,总宽度为 970 像素。项目的数量是动态的(有时是 6、7 或 5)。我遇到的主要问题是我当前的方法:
$(document).ready(function () {
// Navigation Tab Width Calculation
var nCnt = $("#nav > li").length;
var navConstraint = 970 - (nCnt - 1);
var nWidth = navConstraint / nCnt;
//// Sets Distributed Width of items
$("#nav > li > a").css({ "width": nWidth + "px" });
//// Sets Width of ul in submenus
$("#nav ul").css({ "width": nWidth + "px" });
//// Sets Width of li in submenus
$("#nav ul li").css({ "width": nWidth + "px" });
//// Sets Width of a tags in submenus width an adjustment for padding
$("#nav ul li a").css({ "width": nWidth - 10 + "px" });
/// Width Fixes for first and last nav elements
$("#nav > li:first").css({ "margin-left": "0px" });
//// Fixes the last item to fit the stock image width, by browser if necessary
var lastOffset = 1;
$("#nav > li a:last").width(nWidth - lastOffset);
//// Adds Vertical Centering for Menu Items
$("#nav").children("li").each(function () {
var nh = ($(this).find("a").find(".navText").height());
if (nh < 20) {
$(this).find(".navTextPadder").height(11);
}
else {
$(this).find(".navTextPadder").height(7);
}
});
我遇到的问题是 IE 浏览器也不能处理宽度,不确定它们是否不能处理小数宽度,但它比在 FF 中,这非常准确。有更好的方法吗?
I am creating a navigation menu where each menu item needs to be equal width, out of total width of 970px. The amount of items will be dynamic (Sometimes 6, 7 or 5). The main issue I am having is that my current method of doing it:
$(document).ready(function () {
// Navigation Tab Width Calculation
var nCnt = $("#nav > li").length;
var navConstraint = 970 - (nCnt - 1);
var nWidth = navConstraint / nCnt;
//// Sets Distributed Width of items
$("#nav > li > a").css({ "width": nWidth + "px" });
//// Sets Width of ul in submenus
$("#nav ul").css({ "width": nWidth + "px" });
//// Sets Width of li in submenus
$("#nav ul li").css({ "width": nWidth + "px" });
//// Sets Width of a tags in submenus width an adjustment for padding
$("#nav ul li a").css({ "width": nWidth - 10 + "px" });
/// Width Fixes for first and last nav elements
$("#nav > li:first").css({ "margin-left": "0px" });
//// Fixes the last item to fit the stock image width, by browser if necessary
var lastOffset = 1;
$("#nav > li a:last").width(nWidth - lastOffset);
//// Adds Vertical Centering for Menu Items
$("#nav").children("li").each(function () {
var nh = ($(this).find("a").find(".navText").height());
if (nh < 20) {
$(this).find(".navTextPadder").height(11);
}
else {
$(this).find(".navTextPadder").height(7);
}
});
The problem I run into is that IE browsers don't handle the widths as well, not sure if they can't handle decimal widths or not but it is shorter than in FF which is pretty damn exact. Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Math.floor 或其他东西来四舍五入你的数字。
另一种方法是使用一个好的旧表:
Use Math.floor or something to round your numbers.
Another way to do it is to use a good old table :