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.
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.
async
/await
is supported)document.body
elementYou 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>
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.
You can manually scroll any element on a page and optionally detect when done. Just make sure the element you want to scroll has:
height
css property.overflow
property that is set to hidden
.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!
});
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
}
);
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.
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
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''} ) |
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) |
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''} ) |
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 . |
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/.
Run tests:
npm install
npm test
您可以在两个u-scroll-list组件的同一个父组件中,通过v-on指令监听滚动事件,在滚动事件的回调函数中,通过JavaScript操作两个u-scroll-list组件的scrollTop属性,使它们同时滚动。下面是一个示例代码: <template> <div> <u-scroll-list ref="scrollList1" @scroll="handleScroll"/>
大致思路 通过判断滚动高度来确定是否应该开启刷新,如果滚动高度大于0就关闭下拉刷新, 如果滚动高度小于等于0就开启下拉刷新。 html // <template> //scroll-y:开启y轴滚动 //refresher-enabled:动态判断是否开启自定义下拉刷新 //refresher-triggered:动态设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表
微信小程序scroll-view的scroll-into-view无效如何解决 最近在写小程序项目遇到这么一个问题:在使用scroll-into-view的时候无效。 在网上查了一遍,给出的答案有: 1.给scroll-view要设置高度,必须设置上scroll-y或者scroll-x为true(必须要的) 2.scroll-into-view初始化设置的时候,可能因为页面或者数据未加载不能跳转。
1、npm安装 npm install @jiaminghi/data-view 2、main.js引入 import DataV from ‘@jiaminghi/data-view’ Vue.use(DataV) <dv-scroll-board :config="config" @click="boardClick" style="width: 100%; height: 200px" /
如果要修改scroll-view组件并使两个scroll-view组件在滑动一个时同步滑动,可以使用以下方法: 给两个scroll-view组件分别绑定滑动事件,在滑动一个scroll-view组件时,通过代码设置另一个scroll-view组件的滑动位置。 使用JavaScript代码监听一个scroll-view组件的滑动事件,在滑动时动态更新另一个scroll-view组件的滑动位置。 以下
<template> <view class="content"> <scroll-view scroll-y="true" :scroll-into-view="intoindex" style="height: 300px;"> <view v-for="(item,index) in content" :key='index' :id='"text"+index'>{{item+
view <dv-scroll-board :config="config2" style="height:286px;font-size:20px !important;overflow:hidden;" /> data config2: { data: [ [ '<span style="font-size:1rem;">
lastChatData定义的要跳转元素的id app上不生效或者没有跳到合适位置,只需要加一个一次性定时器来延迟赋值就ok。 如: setTimeout(()=>{ that.lastChatData = 元素id },500)
<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),触