Blogger Information
Blog 10
fans 1
comment 0
visits 6569
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
获取滚动值案例_2019-01-22
红色熊猫的博客
Original
655 people have browsed it

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jq_3.3.1_mi.js"></script>
    <style>
        body{margin: 0 ;padding: 0; color: red;text-align: center}
        .nav{width: 100%;height: 50px;background-color: cyan;position: fixed;top: 0}
        .nav2{background-color: red}
        .banner{width: 80%;height:200px;background-color:darkblue;;margin: 0 auto;margin-top: 50px}
        .content{width: 90%;height:1000px;background-color: darkgrey;;margin: 0 auto}
    </style>
</head>
<body>
    <div class="nav">nav</div>
    <div class="banner">banner</div>
    <div class="content">content</div>

    <script>
        $(function(){//JQ就绪函数 写法 固定
            $(window).scroll(function(){//滚动事件
                //scrollTop 获取window下拉距离顶部的高度
                 var top=$(window).scrollTop();
                 if(top>0){
                    $('.nav').css('display','none');
                 }else{
                    $('.nav').css('display','block');
                 } 

                 if(top>250){
                    $('.nav').addClass('nav2').css({'display':'block','color':'#fff'});
                 }else{
                    $('.nav').removeClass('nav2');//清除掉增加的class类
                 }
            });
        })
    </script>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post