Position a Div “Fixed” Vertically and “Absolute” Horizontally within a “Position:Relative” Container Div

后端 未结 3 1670
梦毁少年i
梦毁少年i 2020-12-19 00:42

I\'m looking for a way I can create a div which will be fixed on the page vertically, so if the user scrolls down, the div stays at the same place on the page. But have it p

3条回答
  •  一个人的身影
    2020-12-19 01:25

    With JQuery, use the scrollLeft() property of the document! This would work

    $(window).scroll(function(event) {
       $("#blue-box").css("margin-left", 400-$(document).scrollLeft());
    });
    

    See also

    http://jsfiddle.net/zhQkq/9/

    Good luck!

    Edit: If you want it to use your preset margin-left instead of a hard-coded "400", use

    $(window).scroll(function(event) {
       $("#blue-box").css("margin-left", $("#blue-box").css("margin-left")-$(document).scrollLeft());
    });
    

提交回复
热议问题