1
CSS不支持算法,如margin-left: -60px + 50% of the width of the parent div
。但我的願望是壓倒性的。有誰知道使用JavaScript或其他方式的解決方案嗎?如何使用算術設置樣式屬性?
感謝
邁克
CSS不支持算法,如margin-left: -60px + 50% of the width of the parent div
。但我的願望是壓倒性的。有誰知道使用JavaScript或其他方式的解決方案嗎?如何使用算術設置樣式屬性?
感謝
邁克
試試這個:
var elem = document.getElementById("foobar"),
parent = elem.parentNode;
while (parent && parent.nodeName != "DIV") {
parent = parent.parentNode;
}
if (parent.nodeName == "DIV") {
elem.style.marginLeft = - 60 + (parent.style.width * 0.5) + "px";
} else {
elem.style.marginLeft = "-60px";
}
這當然是可能的JS,但具體代碼將根據您所使用的(香草,框架等)不同。這裏有一個jQuery的例子:
$('#div').css('width', (-60+($('#div').parent().width()*.5))+'px');
有沒有辦法獲得類,而不是ID?再次感謝。 – Mike 2009-07-25 17:59:42