插件 jQuery.scrollex 中文API文档

🌙
手机阅读
本文目录结构

插件 jQuery.scrollex 中文API文档 https://github.com/ajlkn/jquery.scrollex

Nifty scroll events for jQuery

Adds slick new scroll events to jQuery (like enter and leave) so you can drop scrolling effects like a boss. Requires jQuery 1.11+.

Usage

Load up jquery.scrollex.min.js (after jQuery):

<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="jquery.scrollex.min.js"></script>

Then call scrollex() on a selector with a configuration object, which is where you’ll associate handlers with the events you want to use and set various Scrollex options (see Configuration Reference below). For example, this uses the enter and leave events to change the background color of #foobar to green when we scroll within its boundaries (its contact area), then back again when we scroll out of it:

$(function() {
  $('#foobar').scrollex({
    enter: function() {

      // Set #foobar's background color to green when we scroll into it.
        $(this).css('background-color', 'green');

    },
    leave: function() {

      // Reset #foobar's background color when we scroll out of it.
        $(this).css('background-color', '');

    }
  });
});

Events

Scrollex supports the following events:

enter

Triggered when the viewport enters an element’s contact area. Behavior can be tweaked using the mode, top, and bottom options (see next section).

leave

Triggered when the viewport leaves an element’s contact area. Behavior can be tweaked using the mode, top, and bottom options (see next section).

initialize

Triggered as soon as scrollex() is called on an element.

terminate

Triggered as soon as unscrollex() is called on an element, which is used to gracefully undo a previous scrollex() call.

scroll

Triggered as the viewport scrolls through an element. The handler associated with this event is called with a normalized value representing how far the viewport has scrolled through the element (between 0 and 1, although values outside this range are possible if the viewport is above or below the element). For example:

$(function() {
  $('#foobar').scrollex({
    scroll: function(progress) {

      // Progressively increase #foobar's opacity as we scroll through it.
        $(this).css('opacity', Math.max(0, Math.min(1, progress)));

    }
  });
});

mode, top, and bottom

Events that depend on the viewport’s position relative to an element’s contact area (currently just enter and leave) can be further tweaked using the mode, top, and bottom options.

mode

This determines the rules Scrollex uses to figure out when the viewport is considered “inside” or “outside” an element’s contact area. Can be any of the following:

Value Behavior
default Element’s contact area must fall within the viewport.
top Top viewport edge must fall within the element’s contact area.
bottom Bottom viewport edge must fall within the element’s contact area.
middle Midpoint between top/bottom viewport edges must fall within the element’s contact area.

top and bottom

These let you “pad” the edges of an element’s contact area using either a pixel value (150) a percentage of that element’s height (25%), or a percentage of the viewport’s height (20vh). Positive values work inward and shrink the contact area, while negative values work outward and expand the contact area. For example, this expands the contact area of #foobar by 20% of its height in both directions, resulting in enter triggering a bit earlier and leave a bit later:

$(function() {
  $('#foobar').scrollex({
    top: '-20%',
    bottom: '-20%',
    enter: function() {
      $(this).css('background-color', 'green');

    },
    leave: function() {
      $(this).css('background-color', '');
    }
  });
});

Configuration Reference

Name Type Default Description
enter function null Enter event.
leave function null Leave event.
initialize function null Initialize event.
terminate function null Terminate event.
scroll function null Scroll event.
mode string default Mode (default, top, bottom, or middle).
top integer, string 0 Top padding (in pixels, %, vh).
bottom integer, string 0 Bottom padding (in pixels, %, or vh).
delay integer 0 Delay (in ms) between position checks.

License

Scrollex is released under the MIT license, so go nuts.

Copyright © @ajlkn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了