0
我想添加類和removeClass基於頂部滾動與jQuery的div。jQuery scrollTop與「其他如果」語句不起作用
我創建了這兩個變量:
var mainHeight = $('#main-image').height()/1.10;
var footHeight = $('body').height() - $('footer').height();
這是不工作的腳本:
$(window).scroll(function(){
if ($(window).scrollTop() > mainHeight) {
$('#box_centrale').removeClass('chat-bottom, chat-top');
} else if ($(window).scrollTop() > footHeight) {
$('#box_centrale').removeClass('chat-bottom');
$('#box_centrale').addClass('chat-top');
} else {
$('#box_centrale').removeClass('chat-top');
$('#box_centrale').addClass('chat-bottom');
}
});
「如果」和「其他」聲明的作品,但「否則,如果」聲明不起作用...
你知道爲什麼不起作用嗎?
(我爲我的英語道歉)
定義「不起作用」。你在什麼時候設置了「mainHeight」和「footHeight」的值?當你調試它時,它們是你期望的嗎? –
「if」語句是否合理排列?例如,你應該首先檢查「footHeight」嗎?因爲它聽起來像'mainHeight'總是會比'footHeight'大,除非你有一個非常大小的腳。 –
.removeClass()方法需要刪除空格分隔的類列表,而不是逗號分隔。也許這就是它... – Flyer53