2013-05-25 33 views
1

我有這樣的:配方的條件if語句

$(function() { 
    if() { 
     $('#content').css('position', 'fixed'); 
    }); 
}); 

所以,現在我想: 如果「#內容」的「頂」是0,我想是倒寫的CSS在發生的功能。但是我不知道如何在'if'之後的括號之間寫下來。

也許有人可以幫助我這個基本的if語句?

非常感謝提前!

編輯-----------------------------

似乎工作好,雖然仍然有一個問題。我現在有這樣的:

$(function() { 
    if ($('#content').offset().top == 0) { 
     $('#content').css({'position' : 'fixed'}); 
    } 
    else { 
     $('#content').css({'position' : 'relative'}); 
    } 
}); 

這:

$(document).ready(function() { 
    $('#content').scrollTop('100%'); 
}); 

但現在的格立即被 '固定' ..

回答

1

FIDDLE

使用$('selector').offset().top可以獲取頂部位置的數值。

if ($('#content').offset().top == 0) { 
    $('#content').css('position', 'fixed'); 
}); 

獲取更多信息,您可以看到this

還設置你的css狀況這樣

$('#content').css({'position' : 'fixed'}); 

在您的滾動的情況下到上,你可以像這樣

$('#content').scrollTop(yourtopvalue); // your top value goes here. 

對於動畫效果,你可以這樣做也是

$('selector').animate({scrollTop:$('#content').offset().top}, 'slow'); 

您可以使用jQuery混合這些東西,就像這些方式一樣。

+0

你的意思是代替if語句嗎? – Luc

+0

我更新了我的答案 –

+0

謝謝!這部分現在無論如何工作..也許你可以幫助我一個額外的問題:我如何鏈接我的div'頂部'滾動?以便它知道我的div何時滾動到頂部(頂部== 0) – Luc