How to make div follow scrolling smoothly with jQuery?

后端 未结 10 2382
我寻月下人不归
我寻月下人不归 2020-11-28 19:24

In my container there are sections/boxes, but the last one of these boxes should follow scrolling when none of the other boxes are visible.

So, when user sc

10条回答
  •  甜味超标
    2020-11-28 19:54

    This is my final code .... (based on previous fixes, thank you big time for headstart, saved a lot of time experimenting). What bugged me was scrolling up, as well as scrolling down ... :)

    it always makes me wonder how jquery can be elegant!!!

    $(document).ready(function(){
    
        //run once
        var el=$('#scrolldiv');
        var originalelpos=el.offset().top; // take it where it originally is on the page
    
        //run on scroll
         $(window).scroll(function(){
            var el = $('#scrolldiv'); // important! (local)
            var elpos = el.offset().top; // take current situation
            var windowpos = $(window).scrollTop();
            var finaldestination = windowpos+originalelpos;
            el.stop().animate({'top':finaldestination},500);
         });
    
    });
    

提交回复
热议问题