Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何修复滚动后的固定边栏_Jquery_Html_Css - Fatal编程技术网

Jquery 如何修复滚动后的固定边栏

Jquery 如何修复滚动后的固定边栏,jquery,html,css,Jquery,Html,Css,我在尝试滚动后修复侧边栏时遇到了这个问题 在posScroll大于侧栏偏移量之后,我想要保持固定的类继续回到它的原始位置,然后我想要它在哪里,我应该如何修复它 $(window).scroll(function() { var posScroll = $(window).scrollTop(); var sidebar = $('.noticias-search').offset().top; console.log(sidebar,posScroll);

我在尝试滚动后修复侧边栏时遇到了这个问题

posScroll
大于
侧栏
偏移量之后,我想要保持固定的类继续回到它的原始位置,然后我想要它在哪里,我应该如何修复它

$(window).scroll(function() {

    var posScroll = $(window).scrollTop();


    var sidebar = $('.noticias-search').offset().top;
    console.log(sidebar,posScroll);

    if( posScroll  > sidebar) {
        $('.noticias-search').addClass('sidebar-fixed');
    }else{
        $('.noticias-search').removeClass('sidebar-fixed');
    }

});
试一试

var sidebar = $('.noticias-search').height();

function stickySearch(){
    var posScroll = $(window).scrollTop();
    if( posScroll  > sidebar) {
        $('.noticias-search').addClass('sidebar-fixed');
    }else{
        $('.noticias-search').removeClass('sidebar-fixed');
    }
} 

$(window).scroll(function() {

    stickySearch();

});
边栏高度是一个固定值(我假设),您测量它的高度,然后将该高度与
scrollTop()
偏移量进行比较。如果它更大,那么你得到类,如果不是,类被移除

试试看它是否有效

还可以在问题中添加一些html。

尝试使用

var sidebar = $('.noticias-search').height();

function stickySearch(){
    var posScroll = $(window).scrollTop();
    if( posScroll  > sidebar) {
        $('.noticias-search').addClass('sidebar-fixed');
    }else{
        $('.noticias-search').removeClass('sidebar-fixed');
    }
} 

$(window).scroll(function() {

    stickySearch();

});
边栏高度是一个固定值(我假设),您测量它的高度,然后将该高度与
scrollTop()
偏移量进行比较。如果它更大,那么你得到类,如果不是,类被移除

试试看它是否有效

还可以在问题中添加一些html