fixed header on scroll not working in ie8

2019-06-22 14:29发布

问题:

i have implemented a jquery for fix header when user scrolls down the header will get fix position. But it is working in all browser except ie8. could anyone suggest the correct way.

$(document).ready(function()
{
    $(document).scroll(function()
    {
        var window_y = $(window).scrollTop();
        var header_h = $('.header').height();
        if(window_y > header_h)
        {
            $(".header").addClass('fixed');
        }
        else
        {
            $(".header").removeClass('fixed');
        }
    });
});

Working reference is here.

回答1:

try this and this will work fine or also you can give fixed height of header without make variable of header pls try first console

$(window).scroll(function () {
    var header_h = $('.header').height();
    //console.log(header_h);
    if ($(this).scrollTop() > header_h) 
    {
        $(".header").addClass('fixed');
    } 
    else 
    {
        $(".header").removeClass('fixed');
    }
});

let me know if you have any issue when you try this