ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Waypoints

2022-02-26 18:35:01  阅读:181  来源: 互联网

标签:function Waypoints element handler Waypoint waypoint


Waypoints

Getting Started

The first thing you'll need to do is download Waypoints. The lib/ directory contains builds for jQuery and Zepto, as well as a version with no framework dependencies. Choose the one that fits your needs and include it.

<script src="/path/to/noframework.waypoints.min.js"></script>

The rest of this guide and any code snippets on this site, unless otherwise noted, will be using the no-framework build. If you're interested in the goodies that come with jQuery and Zepto builds, take a look at the jQuery/Zepto guide. Code snippets for the no-framework build will work in other builds.

A Basic Waypoint

With Waypoints included, we now have access to a global Waypoint class. We create waypoints by instantiating this class. When creating a new Waypoint we must pass it an options object. There are many properties you can set on this options object, but two of them are required, element, and handler.

var waypoint = new Waypoint({
  element: document.getElementById('basic-waypoint'),
  handler: function() {
    notify('Basic waypoint triggered')
  }
})

The element tells Waypoints which DOM element's position to observe during scroll, and handler is the function that will trigger when the top of that element hits the top of the viewport.

Directions

You may notice the basic example above triggers when we scroll through the waypoint both downwards and upwards. What if we want to perform different actions when scrolling up, or limit our handler to one direction? When a waypoint is triggered, the handler function is passed a direction parameter.

var waypoint = new Waypoint({
  element: document.getElementById('direction-waypoint'),
  handler: function(direction) {
    notify('Direction: ' + direction)
  }
})

Offsets

By default a waypoint triggers when the top of the element hits the top of the window. What if we want it to trigger when the element is 20px from the top instead?

var waypoint = new Waypoint({
  element: document.getElementById('px-offset-waypoint'),
  handler: function(direction) {
    notify('I am 20px from the top of the window')
  },
  offset: 20 
})

The offset option can take a variety of different values to help us control where a waypoint is triggered. For more information, check out the offset option documentation.

this?

When we're inside the handler function the this keyword is a reference to the Waypoint instance. We can use this object to access all the properties and methods in the API. One of the most useful properties is element, the waypoint's DOM element.

var waypoint = new Waypoint({
  element: document.getElementById('element-waypoint'),
  handler: function(direction) {
    notify(this.element.id + ' triggers at ' + this.triggerPoint)
  },
  offset: '75%'
})

Now What?

This has been a very brief guide to the basics of Waypoints. The API documentation goes into more depth about all of the options, methods, and properties available to us. In addition to being a reference for more technically savvy developers, each of the API pages acts as a guide for how to use that feature. The docs are intended to be useful for beginners and experts alike.

If you already have a use for Waypoints in mind, take a moment to check out the Shortcuts section. These shortcut scripts take some of the most common uses of Waypoints and packages them into ready-made extensions.

标签:function,Waypoints,element,handler,Waypoint,waypoint
来源: https://www.cnblogs.com/sexintercourse/p/15940090.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有