当前位置: 首页 > 软件库 > 程序开发 > >

scroll-js

授权协议 MIT License
开发语言 JavaScript
所属分类 程序开发
软件类型 开源软件
地区 不详
投 递 者 林英锐
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Scroll

A light-weight library that will allow you to scroll any html element using native javascript.In addition to providing extra scrolling features, this library also aims to be a polyfill for the scrollToand scrollIntoView APIs and allows you to scrollusing animations that are based loosely on thescrollOptions of the DOM specification.Manipulates native scroll properties so that native events fire appropriately and uses browser's animation frames forfast and smooth rendering.

Why use this over other scroll libraries and plugins?

Many other scroller libraries use absolutely positioning, css animations, transitions and other types of workarounds directly onthe window.document, <html>, <body> and other elements to "fake" a scrolling effect in order to get the scroller to behave.

While this is clever, desktop and mobile devices (mobile mainly), heavily depend on the natural scroll events of theseelements to do helpful things for the user.Like hiding the location url bar as you scroll down the window of the document (on mobile browsers), for instance. Orpausing heavy processes, untilthe user is done performing a task as to not interrupt them, or adding inertia or natural momentum when scrolling. Soit's increasingly important that the scroll logic added to these elements is done in a way thatlends nicely to these use cases, which is what this library does.

Benefits

  • pure, native javascript
  • returns Promises so you can do things when scrolling completes (async/await is supported)
  • no css transitions, animations or absolute positioning hacks
  • manually scroll to any portion of a page and detect when done
  • safe to use on the document.body element
  • supports scroll options from CSS DOM specification
  • battery-friendly -- uses minimal amount of CPU power (no processing on inactive tabs, and hidden elements!)
  • fast and smooth rendering (no choppiness)
  • does not hijack native browser functionality (i.e. inertia scrolling, momentum defaults)
  • native "onscroll" events can still be used (window.onscroll andElement.onscroll)
  • Both non-AMD and AMD compatible

Installation

You can install the library as a npm module by running the following command:

npm i scroll-js

Alternatively, you can simply download one of the distribution files (un-minified or minified version) in the /dist folder and reference them directly in your html file.

<script src="node_modules/scroll-js/dist/scroll.js"></script>

Usage

import { scrollTo } from 'scroll-js';

scrollTo(window, { top: 500 }).then(function () {
    // window has scrolled 500 pixels down the page
});

In addition to the samples below, you can find more in the examples folder.

Scrolling an element

You can manually scroll any element on a page and optionally detect when done. Just make sure the element you want to scroll has:

  1. A specified height css property.
  2. css overflow property that is set to hidden.
  3. Content that extends beyond the specified height.

The following example scrolls the window (document body).

import { scrollTo } from 'scroll-js';
scrollTo(document.body, { top: 500 }).then(function () {
    //scrolling down 500 pixels has completed!
});

Scroll to an element

import { scrollIntoView } from 'scroll-js';
var myElement = document.body.getElementsByClassName('my-element')[0];
scrollIntoView(myElement, document.body, { behavior: 'smooth' }).then(
    function () {
        // done scrolling document's body to show myElement
    }
);

Scroll easing

You can scroll with easing using the behavior option of the scrollTo specification.

import { scrollTo } from 'scroll-js';
scrollTo(document.body, { top: 600, behavior: 'smooth' }).then(function () {
    // scrolled down 600 pixels smoothly
});

Easing is also supported simply by passing the easing option with an easing string that can be found in thesrc/scroll.ts file.

import { scrollTo } from 'scroll-js';
scrollTo(document.body, { top: 200, easing: 'ease-in-out' }).then(function () {
    // scrolled down 200 pixels, easing on beginning and end
});

Note that even though easing option is supported by this package,it is not guaranteed that it will be supported by the specification.

Detect scroll events

Listen in on native scroll events the same way you would if a user was scrolling with a mouse or touch event.

import { scrollTo } from 'scroll-js';
window.addEventListener('scroll', function () {
    // scrolling!
});
scrollTo(document.body, { top: 300 }); // scroll to trigger event

API Documentation

scrollTo(element, options)

Option Type Description
element HTMLElement The element to scroll
options ScrollToOptions A set of scroll options (see writeup below) (i.e. {behavior: 'smooth', top: '20', left: '0''})

scrollTo Options

The scrollTo method allows a set of options which are synonymous with theScrollToOptions of the CSS specification,but some additional ones are provided by this library until supported natively.

Option Type Description
behavior String The type of scroll behavior which can be set to auto or smooth. This is the recommended option since this is already natively supported. If this is set, all other options are ignored.
duration Number The number of milliseconds the scroll will take to complete
easing String The easing to use when scrolling. Only keyword values of the animation-timing-function are supported. But passing function values will eventually be supported also (ie. cubic-bezier(0.1, 0.7, 1.0, 0.1), steps(4, end), etc)

scrollIntoView(element, [scroller], [options])

Option Type Description
element HTMLElement The element to scroll into the viewport
scroller HTMLElement The element to be scrolled (defaults to document.body)
options ScrollIntoViewOptions A set of scroll options to scroll the element into view (see writeup below) (i.e. {behavior: 'smooth', top: '20', left: '0''})

scrollIntoView Options

A set of ScrollIntoViewOptions can be passed to the scrollIntoView method.

Option Type Description
behavior String The type of scroll behavior which can be set to auto or smooth. Defaults to auto.

Examples

Code samples showing how to use this package can be found in the examples folder. To run them, pull down this projectand

npm start

Which will make the examples available at http://localhost:9383/examples/.

Development

Run tests:

npm install
npm test
 相关资料
  • <ion-scroll scrollX="true"> </ion-scroll> <ion-scroll scrollY="true"> </ion-scroll> <ion-scroll scrollX="true" scrollY="true"> </ion-scroll> 属性 属性名称 类型 描述 scrollX boolean 是否启用x轴滚动 scrollY boolean 是否启用

  • 滚动列表,提供了优质的原生滚动体验,便捷的配置项和事件,是一个基于better-scroll进行封装的组件。 滚动原理 由于 better-scroll 的滚动原理为:在滚动方向上,第一个子元素的长度超过了容器的长度。 那么对于 Scroll 组件,其实就是内容元素.cube-scroll-content在滚动方向上的长度必须大于容器元素 .cube-scroll-wrapper。根据滚动方向的不

  • 无限滚动指令。 引入 import { InfiniteScroll } from 'mint-ui'; Vue.use(InfiniteScroll); 例子 为 HTML 元素添加 v-infinite-scroll 指令即可使用无限滚动。滚动该元素,当其底部与被滚动元素底部的距离小于给定的阈值(通过 infinite-scroll-distance 设置)时,绑定到 v-infinite

  • 描述 (Description) 您可以通过添加.scroll类来水平滚动表。 例子 (Example) 以下示例演示了在Foundation中使用scroll table - <!doctype html> <head> <meta charset = "utf-8" /> <meta http-equiv = "x-ua-compatible" content =

  • 介绍 (Introduction) 滚动条控件表示滚动条组件,以便用户可以从值范围中进行选择。 类声明 以下是java.awt.Scrollbar类的声明: public class Scrollbar extends Component implements Adjustable, Accessible 字段 (Field) 以下是java.awt.Image类的字段: st

  • scroll-view 可滚动视图区域。 属性名 类型 默认值 说明 scroll-x Boolean false 允许横向滚动 scroll-y Boolean false 允许纵向滚动 upper-threshold Number 50 距顶部/左边多远时(单位px),触发 scrolltoupper 事件 lower-threshold Number 50 距底部/右边多远时(单位px),触