jQuery實現頁面滾動時頂部動態顯示隱藏

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery實現頁面滾動時頂部導航顯示或者隱藏</title>
<style>
p{ line-height:200%; text-align:center;}
.top-title {background:#e74c3c;color:white;font-size:24px;padding:5px;text-align:center;position: fixed;left:0;top:0;width:100%;transition: top .5s;}
.hiddened{top: -90px;}
.showed{top:0;z-index: 9999;}
.margint{height:1500px; padding-top:150px; font-size:28px;}
</style>
</head>
<body>
<div class="top-title">這是頂部導航條</div>
 <div class="margint"><p>滾動看效果</p><p>滾動看效果</p></div>
<script src="http://libs.useso.com/js/jquery/1.11.1/jquery.min.js"></script>
<script>
$(function(){    
    var winHeight = $(document).scrollTop();

    $(window).scroll(function() {
        var scrollY = $(document).scrollTop();// 獲取垂直滾動的距離,即滾動了多少

        if (scrollY > 550){ //若是滾動距離大於550px則隱藏,不然刪除隱藏類
            $('.top-title').addClass('hiddened');
        }
        else {
            $('.top-title').removeClass('hiddened');
        }

        if (scrollY > winHeight){ //若是沒滾動到頂部,刪除顯示類,不然添加顯示類
            $('.top-title').removeClass('showed');
        }
        else {
            $('.top-title').addClass('showed');
        }                

     });
});
</script>
</body>
</html>



轉載自:http://www.jqcool.net/jquery-scroll.html
html