[英]jQuery scrollTop with “else if” statement doesn't work
I'm trying to addClass and removeClass to a div based on top scrolling with jquery. 我正在尝试基于jquery的顶部滚动将addClass和removeClass添加到div中。
I created these two variables: 我创建了以下两个变量:
var mainHeight = $('#main-image').height()/1.10;
var footHeight = $('body').height() - $('footer').height();
And this is the script that doesn't works: 这是不起作用的脚本:
$(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');
}
});
The "if" and "else" statement works but "else if" statement doesn't work... “ if”和“ else”语句有效,但“ else if”语句不起作用...
Have you any idea why does not work? 你知道为什么不起作用吗?
(I apologize for my English) (我为我的英语道歉)
I solved! 我解决了! There was an error in the second variable, missing parentheses to the operation.
第二个变量中有一个错误,该操作缺少括号。 I decide to create a jsfiddle with an example of what I wanted to do, hoping that it will help someone in the future.
我决定创建一个jsfiddle,并提供一个我想做的事的示例,希望它对以后的人有所帮助。
Thank you all 谢谢你们
Here the link: JSFiddle 这里的链接: JSFiddle
$(document).ready(function(e) {
var mainHeight = $('#header').height()/2;
var footHeight = ($('body').height() - $('#footer').height() - 290);
$(window).scroll(function(){
if ($(window).scrollTop() < mainHeight) {
$('#fixed-box').css({
"bottom" : "calc(100% - 310px)",
});
} else if ($(window).scrollTop() > footHeight) {
$('#fixed-box').css({
"bottom" : "calc(260px)",
});
} else {
$('#fixed-box').css({
"bottom" : "calc(50% - 25px)",
});
}
});
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.