我有一個div容器,它的頂邊應該被設置爲$(window).height()的依賴關係,所以首先我嘗試了一些東西像這樣:
$("div#outerstart").css("margin-top",$(window).height() -805+"px");
它工作得很好,但上邊距不應該是負的,所以我想這:
$("div#outerstart").css("margin-top",function() {
if ($(window).height() < 805) {
0+"px"
} else {
$(window).height() -805+"px"
}
});
或也
if ($(window).height() < $(document).height()) {...
但它顯示沒有效果並且未設置邊距頂部。你有什麼建議嗎?
使用關鍵字return?其功能,而不是直接聲明 –
@Marek Sebera:寫一個答案,而不是評論? – thirtydot