简体   繁体   中英

jquery scrolling function not working properly

I'm using this code to change position when scroll. The problem is when scrolled to top of the page css top:'0px' not working. Here is the code.

window.onload = function() {

    var stickySidebar = $('.bk-form-wrap').offset().top;
    var $div = $('div.bk-form-wrap');
    $(window).scroll(function() {

        if ($(window).scrollTop() > stickySidebar) {
            $div.css({
                 position:'fixed',
                 height: '70px'
            });
            $div.animate({
                top: '95px',
                //top:'100%',
               // marginTop: - $div.height() 
            });
        }
        else {  
        }   

        if ($(this).scrollTop() == 0) {
             //Call your event here
             $div.css({
                 position:'relative',
             });
             $div.animate({
                top:'0px',
             });
        }
    });
};

And link to page . Plese help. Thanks.

Try this.

var $div = $('div.bk-form-wrap');  
$(window).scroll(function() {
    var stickySidebar = $('.bk-form-wrap').offset().top;
    if ($(window).scrollTop() > stickySidebar) {
         $div.css({
             position:'fixed',
             height: '70px'
              },1000);
        $div.animate({
            top: '95px'
            //top:'100%',
           // marginTop: - $div.height() 
        });
    }

    else if ($(window).scrollTop() == 0) {
       //Call your event here
         $div.css({
             position:'relative'
              });
           $div.animate({
            top: '0px'
        },500);
    }   
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM