当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS库之Waypoints的用法详解

JS库之Waypoints的用法详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

一款用于捕获各种滚动事件的插件?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>

总结

以上所述是小编给大家介绍的js库之waypoints的用法详解,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网