Home >Web Front-end >JS Tutorial >jQuery sidebar scrolling with the window implementation method_jquery

jQuery sidebar scrolling with the window implementation method_jquery

WBOY
WBOYOriginal
2016-05-16 17:41:091265browse
复制代码 代码如下:

$(function() {
    var $sidebar   = $("#sidebar"),  
        $window    = $(window),  
        offset     = $sidebar.offset(),  
        topPadding = 15;  

    $window.scroll(function() {  
        if ($window.scrollTop() > offset.top) {  
            $sidebar.stop().animate({  
                marginTop: $window.scrollTop() - offset.top topPadding  
            });  
        } else {  
            $sidebar.stop().animate({  
                marginTop: 0  
            });  
        }  
    });  

});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn