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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-slider-fjj

v0.0.13

Published

react-slider

Readme

react 轮播图组件

前段时间使用原生 JS 实现了一个轮播图插件,现在在 React 的基础上,重新实现了一个组件。

正文

本文介绍一个基于 React 框架实现的轮播图组件。

需求文档

  • 需求
    • 能够实现自左往右规律性轮播。
    • 轮播到最后一张图时,能够无缝衔接到第一张图。
    • 缩略小圆点能够跟随相对的图片轮播。
    • 鼠标放到图片会停止轮播。
    • 鼠标点击缩略小圆点轮播图也会跳转的相应的图片。
    • 点击图片能够跳转到相对的地址。
  • 开发需求
    • 支持图片和点击链接的配置。
    • 支持轮播过渡时间的配置。
    • 支持轮播间隔的配置。
    • 支持轮播图所在容器的配置。
    • 支持缩略圆点大小、颜色、激活颜色、位置的配置。

设计方案

  • 定义一个轮播图组件类。
  • 实例化一个轮播图对象,接收一些外部属性 props:
    • list
      • 图片对象数组
        • text:图片地址。
        • href:跳转链接。
    • intervalTime
      • 轮播的间隔时间。
    • transitionTime
      • 轮播过渡的间隔时间。
    • width
      • 显示容器或图片的宽度。
    • height
      • 显示容器或图片的高度。

初始化方法做的事

  • 定义一个state对象,用 index 属性计数。

第一次渲染前方法做的几件事情

  • 获取数组的长度
  • 定义显示容器的大小,
  • 定义图片容器的宽度和高度。
  • 添加一个定时器
  • 添加定时器执行的loop方法
  • 在轮播图容器中加一个过度动画结束的事件监听器,每当过渡动画结束时候再启动一个定时器,接着执行loop方法。当轮播到最后一张图片时,设置容器的过度时间为 0 秒,使得容器瞬间跳转到第一张图片所在的位置。index数据也会回到初始值,这样才能达到无缝滚动。

loop方法做的事情

  • 定义了除最后一张图片其他图片的过渡时间。
  • 定义了除最后一张图片其他图片的平移方向大小。
  • 每次轮播完成后index数据就会加1。

渲染方法做的事情

  • 生成的显示容器、轮播图容器以及轮播的的图片。
  • 添加轮播图的点击事件,鼠标左键点击轮播图中的图片时会跳转相应地址链接。
  • 生成缩略小圆点,缩略小圆点会随着轮播图滚动。对应轮播图图片的小圆点为黄色,其余为红色。
  • 添加缩略小圆点的点击事件,当鼠标左键点击到红色缩略小圆点的时候,轮播图也会跳转到相应的图片。

编码实现

  • 定义react组件类的方法。

  • 利用react的componentDidMount生命周期。

  • 利用setTimeout定时器。

  • 利用this.props传递组件所需要的属性。

  • 利用addEventListener添加监听事件。

  • 利用CSS中transform和transition的使用方法。