当前位置: 首页 > 工具软件 > Waypoints > 使用案例 >

waypoints的使用

归翔
2023-12-01

一、最简易的使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>waypoints的最简单使用</title>
    <!-- 定义css样式 -->
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        #example-basic{
            height: 500px;
            text-align: center;
        }
    </style>
    
    <!-- 引入js文件 -->
    <script src="js/jquery-3.0.0.min.js"></script>
    <script src="js/jquery.waypoints.js"></script>
    <script src="js/jquery-ui.min.js"></script>
    
    <!-- 启动waypoints -->
    <script>
    $(function () {
        $('#example-basic').waypoint(function() { 
            console.log("hi,example-basic,你的顶部碰到了浏览器窗口的顶部!");//测试打开web调试器,看控制台信息
        });
    });
    //注:无论是鼠标向上或向下只要该元素的顶部碰到浏览器的顶部都会触发waypoints事件
    </script>

</head>
<body>
    <div style="background:#ccc;height:1800px;">one div</div>
    <div id="example-basic">example-basic.</div>
    <div style="background:#ccc;height:1800px;">one div</div>
</html>
 

二、能检测鼠标滚动方向的基本应用

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>检测鼠标滚动方向</title>
    
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        #example-basic{
            height: 500px;
            text-align: center;
        }
        .in{
            font-size: 36px;
            color: #ff0;
            background:red;
            transition:all 0.5s;
        }
    </style>

    <script src="js/jquery-3.0.0.min.js"></script>
    <script src="js/jquery.waypoints.js"></script>
    <script src="js/jquery-ui.min.js"></script>

    <script>
    $(function () {
        $('#example-basic').waypoint(
            function(direction){ 
                if(direction=="down"){
                    $('#example-basic').addClass("in");
                    $('#example-basic').html("你在向下滚动!")
                }else{
                    $('#example-basic').removeClass("in");
                    $('#example-basic').html("你在向上滚动!")
                }
            },//第1个参数为waypoints事件响应时所执行的代码,是一个匿名函数即可
            {
                offset:'50%'
            }//第2个参数为偏移量,本例即该div到窗口高度一半时触发
            );
    });
    </script>
</head>
<body>
    <div style="background:#ccc;height:1800px;">one div</div>
    <div id="example-basic">example-basic.</div>
    <div style="background:#ccc;height:1800px;">one div</div>
</html>

三、鼠标滚动监听加动画效果1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>加下动画效果的</title>
    
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        div{
            background: #eee;
        }
        .banner{
            width: 1100px;
            margin: 0 auto;
        }
        .title{
            height: 100px;
            background: #9f9;
        }
        .lt{
            position: relative;
            height: 400px;
            border:1px dotted #999;
        }

        .lt_left{
            position: absolute;
            width: 500px;
            height: 300px;
            left: -20%;
            top: 0;
            margin-left: -550px;
            background: #f99;
        }
        .lt_right{
            position: absolute;
            width: 500px;
            height: 300px;
            left: 120%;
            top: 0;
            margin-left: 50px;
            background: #99f;
        }
    </style>

    <script src="js/jquery-3.0.0.min.js"></script>
    <script src="js/jquery.waypoints.js"></script>
    <script src="js/jquery-ui.min.js"></script>

    <script>
    $(function () {
        //获取运动的盒子
        var boxElemets = $('.boxaction');
        $.each(boxElemets, function() {
            $(this).attr('init', 'false');
        }); 
        
        //判断是否出现在浏览器界面里面!
        function isScrolledIntoView(elem) { 
            var docViewTop = $(window).scrollTop();
            var docViewBottom = docViewTop + $(window).height();
            var elemTop = $(elem).offset().top;
            if (elemTop + 50 < docViewBottom) {
                return true
            } else {
                return false
            }
        }

        //定义动画
        function animateInit() {
            $.each(boxElemets, function() {
                if ($(this).attr('init') == 'false' && isScrolledIntoView($(this))) { //没有显示过且刚出现在浏览器界面
                    $(this).attr('init', 'true');
                    $(this).animate({
                        'left': '50%'
                    }, 1000, 'easeOutCubic');
                }
            });
        }

        animateInit(); //先执行一次animateInit
        $(window).scroll(function() { //滑动执行animateInit
            animateInit();
        });
    })
    </script>
</head>
<body>
    <div style="background:#ccc;height:1800px;text-align:center;">top div</div>
    <div class="banner">
        <div class="title">这是标题</div>
        <div class="lt">
            <div class="lt_left boxaction">这是左边盒子</div>
            <div class="lt_right boxaction">这是右边盒子</div>
        </div>
    </div>
</body>
</html>
 

 

四、鼠标滚动监听加动画效果2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>加下动画效果的</title>
    
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        div{
            background: #eee;
        }
        .banner{
            width: 1100px;
            margin: 0 auto;
        }
        .title{
            height: 100px;
            background: #9f9;
        }
        .lt{
            position: relative;
            height: 400px;
            border:1px dotted #999;
            overflow: hidden;
        }

        .l_box{
            position: absolute;
            width: 500px;
            height: 300px;
            left: -20%;
            top: 0;
            margin-left: -550px;
            background: #f99;
            opacity: 0;
            transition:all 1s;
        }
        .r_box{
            position: absolute;
            width: 500px;
            height: 300px;
            left: 120%;
            top: 0;
            margin-left: 50px;
            background: #99f;
            opacity: 0;
            transition:all .3s;
        }
        .active{
            left: 50%;
            opacity: 1;
            transition:all .7s;
        }
        
    </style>

    <script src="js/jquery-3.0.0.min.js"></script>
    <script src="js/jquery.waypoints.js"></script>
    <script src="js/jquery-ui.min.js"></script>

    <script>
    $(function () {
        $(".banner").waypoint(
            function(direction){
                var active_boxs = $('.active_box');
                if(direction=="down"){
                    $.each(active_boxs, function() {
                        $(this).addClass("active");
                    }); 
                }else{
                    $.each(active_boxs, function() {
                        $(this).removeClass("active");
                    }); 
                }
            },
            {offset:'50%'}
        );
    });
    </script>
</head>
<body>
    <div style="background:#ccc;height:1800px;text-align:center;">top div</div>
    <div class="banner">
        <div class="title">这是标题</div>
        <div class="lt">
            <div class="l_box active_box">这是左边盒子</div>
            <div class="r_box active_box">这是右边盒子</div>
        </div>
    </div>
</body>
</html>
 
 类似资料: