JS库之Waypoints的用法详解


一款用于捕获各种滚动事件的插件?Waypoints。同时Waypoints还支持固定元素和无限滚动的功能,功力十分强大。

一、最简易的使用

<!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>

三、鼠标滚动加动画效果的应用

<!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>

总结