JQuery create a scroll follow div

后端 未结 5 1412
情歌与酒
情歌与酒 2021-01-01 07:52

ok I have seen people using position:fixed to have a div follow the scroll. I have also seen the following solution which is good ( Jquery follow scroll ) but

相关标签:
5条回答
  • 2021-01-01 08:12

    This div in the example is not polsition:fixed, or absolute. What they do is to animate the margint-top attribute on scroll relatively

    0 讨论(0)
  • 2021-01-01 08:16

    Also, this is the code in the example page, just to get an idea

     var $scrollingDiv = $("#customize");
        $(window).scroll(function () {
            if ($(window).scrollTop() > 490) {
                if (($("#maincontentbox").height() - $(window).scrollTop()) > 0) {
                    $scrollingDiv.stop().animate({
                        "marginTop": ($(window).scrollTop() - 500) + "px"
                    }, "slow");
                }
            } else {
                $scrollingDiv.stop().animate({
                    "marginTop": "0px"
                }, "slow");
            }
        });
    
    0 讨论(0)
  • 2021-01-01 08:17

    As a slight alternative to Adam Hutchinson's

    http://jsfiddle.net/HelloJoe/JjuQu/

    It's pretty self explanatory but just say if you need anything explained.

    0 讨论(0)
  • 2021-01-01 08:27

    Simpler solution:

    $('#divtomove').scrollFollow();
    
    0 讨论(0)
  • 2021-01-01 08:35

    Looks like you need to map an event to the document scrolling and then move a div relative to the scroll. Something along these lines may give you somewhere to start.

    $(document).scroll(function(){
        $('#divtomove').css('top', $(document).scrollTop());
    })
    
    0 讨论(0)
提交回复
热议问题