npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

caler_scroll_listener

v0.0.4

Published

一个滚动监听工具。

Downloads

16

Readme

简介

这是一个简约的滚动监听工具库。

安装

直接通过 script 标签引入:

<script src="https://unpkg.com/[email protected]/dist/scrollListener-0.0.1.js"></script>

这时会注册一个全局函数 scrollListener

通过 npm 安装:

npm i --save caler_scroll_listener

引入组件中:

const scrollListener = require('caler_scroll_listener');

使用方法

scrollListener 函数接收两个参数,第一个参数为将要监听滚动项的包裹元素,也可以传入一个 css 选择器字符串,函数内部将调用 document.querySelector 获取包裹元素;第二个参数是一个对象,为 scrollListener 的配置参数:

scrollListener('#wrapper', {
  relative: 'wrapper',
  itemSelector: 'h4.title',
  cb (currentItem, currentItemIndex) {
    // do something
  }
})

配置参数

relative

值:viewport | wrapper

默认值:viewport

规定元素滚动时的参照物,viewport 是基于视口,wrapper 是基于元素的包裹容器进行计算。

建议使用 wrapper 模式,viewport 会重复计算元素的 getBoundingClientRect,性能较 wrapper 稍差。

itemSelector

值:监听滚动项目的 CSS 选择器。字符串类型。

默认值:'[data-scroll-item]'

top

默认值:20

当滚动项目距离参照元素顶部多少像素时,就认为当前项目为该项目。

reduceTime

默认值:100

函数内部使用了节流函数以优化 scroll 事件的性能,可通过该选项自定义节流的时间间隔,单位为毫秒。

cb()

值:回调函数。传入两个参数,第一个为当前项目,第二个为当前项目的 index。

默认值:undefined

scrollListener 只提供计算得出当前项目,并不参与实际的业务逻辑,所以要通过cb回调函数实现具体的业务功能,该函数将接收两个参数,分别为当前项目元素,与当前项目元素的 index。

示例:

const navLis = document.querySelector('li.items');
scrollListener('#wrapper', {
  relative: 'wrapper',
  itemSelector: '.title-item',
  cb (currentItem, currentItemIndex) {
    for (let item of navLis) {
      item.classList.remove('active');
    }
    navLis[currentItemIndex].classList.add('active');
  }
})