当我手动滚动页面时,为什么.scroll()不再起作用了?

时间:2018-04-16 08:30:41

标签: javascript jquery html

这是我的代码:

@ConditionalOnMissingBean
@ConditionalOnProperty( value = "my.enabled", havingValue = "false" )
Foo1 foo1(){
return Foo1();
}

@ConditionalOnMissingBean
Foo2 foo2(){
return Foo2();
}

当页面的网址(// ----------------------------------------- Part 1 -------------------------------------- var anchor = location.hash; if(anchor.length){ if (anchor.indexOf("faq") >= 0){ if ( $(anchor).length ) { var elem_question = $(anchor); $(anchor).css('background-color','#FC9A24'); // change background color to orange for 1 sec becase this is selected setTimeout(function() { elem_question.css({"background-color":"transparent", "transition":"background-color 1.5s ease"});}, 400); $('html, body').stop().animate({scrollTop:elem_question.offset().top-80}, '500'); // for scrooling return false; } } } // ----------------------------------------- Part 2 -------------------------------------- doc.scroll(function () { var y = $(this).scrollTop(); if (y > 37) { $("#header").addClass("header_shadow"); } else $("#header").removeClass("header_shadow"); }); 执行)中有锚点(例如#faq-1)时,Part 1不再有效。这意味着我没有看到头部的阴影。我该如何解决这个冲突?

2 个答案:

答案 0 :(得分:0)

你需要触发滚动事件

var anchor = location.hash;
if(anchor.length){
    if (anchor.indexOf("faq") >= 0){
        if ( $(anchor).length ) {
            var elem_question = $(anchor);
            $(anchor).css('background-color','#FC9A24'); // change background color to orange for 1 sec  becase this is selected
            setTimeout(function() { elem_question.css({"background-color":"transparent", "transition":"background-color 1.5s ease"});}, 400);
            $('html, body').stop().animate({scrollTop:elem_question.offset().top-80}, '500'); // for scrooling
            doc.scroll();//trigger the scroll
            return false;
        }
    }

}

答案 1 :(得分:0)

好的,通过更改Part 1Part 2的位置解决了问题。这是工作版本:

// ----------------------------------------- Part 2 --------------------------------------

doc.scroll(function () {
    var y = $(this).scrollTop();

    if (y > 37) {
        $("#header").addClass("header_shadow");
    }

    else
        $("#header").removeClass("header_shadow");
});


// ----------------------------------------- Part 1 --------------------------------------

var anchor = location.hash;
if(anchor.length){
    if (anchor.indexOf("faq") >= 0){
        if ( $(anchor).length ) {
            var elem_question = $(anchor);
            $(anchor).css('background-color','#FC9A24'); // change background color to orange for 1 sec  becase this is selected
            setTimeout(function() { elem_question.css({"background-color":"transparent", "transition":"background-color 1.5s ease"});}, 400);
            $('html, body').stop().animate({scrollTop:elem_question.offset().top-80}, '500'); // for scrooling
            return false;
        }
    }

}

在问题的版本中,我猜测执行顺序会影响滚动并使其禁用。