Keep Side Navigation Fixed with Scrolling of page

后端 未结 3 1911
忘掉有多难
忘掉有多难 2021-02-06 12:45

I have a clients website - www.stagecraft.co.uk and they want the navigation on the hire pages (longer page) to still be there at when you scroll the page down. I\'ve had a quic

相关标签:
3条回答
  • 2021-02-06 13:06

    $(window).scroll(function() { $('#myElement').css('top', $(this).scrollTop() + "px"); });

    I got this from question 257250

    What is the simplest jQuery way to have a 'position:fixed' (always at top) div?

    0 讨论(0)
  • 2021-02-06 13:09

    I would hesitate to use only jquery - I think it's really annoying when scrolling a page and a DIV "jumps" due to javascript updating the position of an element.

    I'd use position:fixed, and just move the position of the box to the top of the left side with javascript when scrolling down initially, and then leave it there. It's a sort of compromise.

    0 讨论(0)
  • 2021-02-06 13:23

    You could make it position fixed, only when scrolling has reached a certain point:

    $(window).scroll(function() {
        if ($(this).scrollTop() > 200) { //I just used 200 for testing
            $("#tester").css({ "position": "fixed", "top": 0 });
        } else {
            $("#tester").css({ "position": "absolute", "top": "200px" }); //same here
        }               
    });
    

    and the CSS for the div is as following:

    #tester {
        position: absolute;
        right: 20px;
        top: 200px;
        height: 200px;
        width: 100px;
        background: red;
    }
    
    0 讨论(0)
提交回复
热议问题