Javascript jquery:if/else语句后面的代码在里面。滚动功能不起作用。

Javascript jquery:if/else语句后面的代码在里面。滚动功能不起作用。,javascript,jquery,Javascript,Jquery,我有一个jquery函数,其中包含了我的网站的所有事件,如果用户滚动,这些事件都需要触发。但是,最后一行代码不起作用,同样console.log()放置在if/else语句之后,则不会激发 $(window).scroll(function(){ //////////////////////////////////////////////////////////////////////////////////////////// // make homepage CTA and rainbow

我有一个jquery函数,其中包含了我的网站的所有事件,如果用户滚动,这些事件都需要触发。但是,最后一行代码不起作用,同样
console.log()放置在if/else语句之后,则不会激发

$(window).scroll(function(){

////////////////////////////////////////////////////////////////////////////////////////////
// make homepage CTA and rainbow bar disappear once top of below mountain hits those divs
////////////////////////////////////////////////////////////////////////////////////////////
if($('.below-the-mountain').offset().top < $('.the-observable-universe').offset().top + 100){
    $('.cta').css('display', 'none');
    $('.rainbow').css('display', 'none');
}else{
    $('.cta').css('display', 'block');
    $('.rainbow').css('display', 'inline-block');
}
////////////////////////////////////////////////////////////////////////////////////////////
// this is the code that does not fire, and it's because of the if/else statement above it
////////////////////////////////////////////////////////////////////////////////////////////
$('.cat-title').css('top', $('.before-shop-loop').offset().top + $('.cat-title').height() / 2 - $('.header').height()); 
});

为什么,在这种情况下,顺序的主要区别/重要性是什么?

我认为您在if/else条件下有脚本错误,请将行
window.onerror=function(e){alert(e)}
放在滚动函数之外,如果有错误,它会提醒您。

您能创建一个小提琴吗?我不能。if/else和另一行与站点上的两个独立页面相关。所以小提琴手不会重复这个问题,我认为if/else语句中的逻辑是正确的。但是,它确实与一个单独的页面相关,因此它会抛出一个
未捕获类型错误:无法读取控制台中未定义的
的属性“top”。未捕获的TypeError会导致函数过早退出吗?此外,控制台比使用警报进行调试要高效得多,仅供参考:)是的,这是导致脚本中断的原因,根据错误
无法读取未定义的属性“top”
,您应该考虑变量,即
var a=$(“.山下”)。offset()
if(a&&a.top>something)
$(window).scroll(function(){

////////////////////////////////////////////////////////////////////////////////////////////
// make title text all parralaxy so it stays in view as long as possible
////////////////////////////////////////////////////////////////////////////////////////////
$('.cat-title').css('top', $('.before-shop-loop').offset().top + $('.cat-title').height() / 2 - $('.header').height());

////////////////////////////////////////////////////////////////////////////////////////////
// make homepage CTA and rainbow bar disappear once top of below mountain hits those divs
////////////////////////////////////////////////////////////////////////////////////////////
if($('.below-the-mountain').offset().top < $('.the-observable-universe').offset().top + 100){
    $('.cta').css('display', 'none');
    $('.rainbow').css('display', 'none');
}else{
    $('.cta').css('display', 'block');
    $('.rainbow').css('display', 'inline-block');
}

});