1 Star 0 Fork 0

KwooShung/react-overlay-scrollbars-smooth

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

React Overlay Scrollbars Smooth

是一款基于 OverlayScrollbarssmoothscroll-for-websites 的整合插件,它隐藏了原生滚动条,提供了自定义样式的覆盖滚动条,并保留了原生功能和感觉,同时具有滚动惯性。

GitHub License GitHub Release Date - Published_At GitHub last commit GitHub code size in bytes GitHub top language GitHub pull requests GitHub issues Github Stars NPM Version Npm.js Downloads/Week Github CI/CD codecov Maintainability Gitee Repo

English | 中文

为什么开发它?

  • 我不喜欢默认的浏览器滚动条,它们看起来很丑,而且在不同的浏览器中看起来也不一样。
  • 他无法在不同的浏览器中保持一致的外观和行为,无法深度自定义;
  • 默认滚动条,无法和你的网站或应用程序的设计风格保持一致;
  • 当需要弹窗时,滚动条会出现在弹窗外部,这样会导致页面的滚动条和弹窗的滚动条同时出现,这样会导致用户体验不佳;
  • 当(Dialog/Modal)弹窗时,需要对body进行padding-right,特别当导航条有position:fixed;的时候还需要对其进行特别处理,而且会产生晃动;
  • 滚动条,在鼠标滚轮滚动时,比较生硬不够顺滑;

安装

npm

npm install @kwooshung/react-overlay-scrollbars-smooth

yarn

yarn add @kwooshung/react-overlay-scrollbars-smooth

pnpm

pnpm add @kwooshung/react-overlay-scrollbars-smooth

使用方法

样式

在某些框架中直接在全局css / less / scss中引入样式即可,如下所示:

@import url('@kwooshung/react-overlay-scrollbars-smooth/dist/react-overlay-scrollbars-smooth.css');

在部分框架中,如 Next.js 中,可能需要加入~符,才行,如下所示:

@import url('~@kwooshung/react-overlay-scrollbars-smooth/dist/react-overlay-scrollbars-smooth.css');

你也可以在全局页面,如 Next.js 中的 Layout 页面 或 对应组件 中引入,如下所示:

import '@kwooshung/react-overlay-scrollbars-smooth/dist/react-overlay-scrollbars-smooth.css';

组件

在某个元素上使用 OverlayScrollbarsSmooth 组件,如下所示:

import { OverlayScrollbarsSmooth } from '@kwooshung/react-overlay-scrollbars-smooth';

// 这里使用的是直接在组件中引入次样式,也可以使用上面其他的方式
import '@kwooshung/react-overlay-scrollbars-smooth/dist/react-overlay-scrollbars-smooth.css';

function App() {
  return (
    <div style={{ height: '300px' }}>
      <OverlayScrollbarsSmooth>
        <div style={{ height: '1000px' }} />
      </OverlayScrollbarsSmooth>
    </div>
  );
}

如果你想让它作用于 Body 上,可以这样做:

import { OverlayScrollbarsSmooth } from '@kwooshung/react-overlay-scrollbars-smooth';

function App() {
  // 这行代码无论写到什么地方,只要 `tag=body`,它就会作用于 `Body` 上,从而替代默认滚动条。别忘记引入样式!
  return <OverlayScrollbarsSmooth tag='body' />;
}

hooks

你可以直接使用 useSmoothScroll 实现平滑滚动,它将会导致所有的滚动条都变得平滑,建议在全局布局之类的地方调用一次即可,如下所示:

import { OverlayScrollbarsSmooth, useSmoothScroll } from '@kwooshung/react-overlay-scrollbars-smooth';
import '@kwooshung/react-overlay-scrollbars-smooth/dist/react-overlay-scrollbars-smooth.css';

function App() {
  useSmoothScroll();

  return (
    <div style={{ height: '300px' }}>
      <OverlayScrollbarsSmooth>
        <div style={{ height: '1000px' }} />
      </OverlayScrollbarsSmooth>
    </div>
  );
}

API

OverlayScrollbarsSmooth

OverlayScrollbarsSmooth 组件支持所有 OverlayScrollbars 的 API,你可以在这里查看 OverlayScrollbars API

属性 说明
tag string,默认值:div,如果是:body,作用于 Body 上,从而替代默认滚动条。等同于 OverlayScrollbarselement
className string,默认值:'',自定义类名;tag=body 时,无效;
options 等同于 OverlayScrollbarsoptions,默认:{scrollbars: {theme: 'os-theme-dark'}}
events 等同于 OverlayScrollbarsevents,默认:{}

useSmoothScroll

支持 smoothscroll-for-websitesexcluded 之外的所有参数,你可以在这里查看 smoothscroll-for-websites API,以下参数同时也是默认值(它和 smoothscroll-for-websites 的默认值略有区别),你可以根据自己的需求进行修改,如下所示

import { OverlayScrollbarsSmooth, useSmoothScroll } from '@kwooshung/react-overlay-scrollbars-smooth';
import '@kwooshung/react-overlay-scrollbars-smooth/dist/react-overlay-scrollbars-smooth.css';

function App() {
  useSmoothScroll({
    // Scrolling Core
    frameRate: 150, // [ms]
    animationTime: 1000, // [ms]
    stepSize: 100, // [px]

    // Pulse (less tweakable)
    // ratio of "tail" to "acceleration"
    pulseAlgorithm: true,
    pulseScale: 4,
    pulseNormalize: true,

    // Acceleration
    accelerationDelta: 50,
    accelerationMax: 3,

    // Keyboard Settings
    keyboardSupport: true,
    arrowScroll: 50,

    // Other
    touchpadSupport: false, // ignore touchpad by default
    fixedBackground: true
  });

  return (
    <div style={{ height: '300px' }}>
      <OverlayScrollbarsSmooth>
        <div style={{ height: '1000px' }} />
      </OverlayScrollbarsSmooth>
    </div>
  );
}

用到的开源项目

感谢项目作者为开源社区的无私贡献,让我们的工作变得更加简单!!!

许可证

MIT

MIT License Copyright (c) 2023 KwooShung 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.

简介

A React scrollbar custom component that hides the native scrollbar, provides a custom style overlay scrollbar, and retains the native functionality and feel while having scrolling inertia. 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/kwooshung/react-overlay-scrollbars-smooth.git
git@gitee.com:kwooshung/react-overlay-scrollbars-smooth.git
kwooshung
react-overlay-scrollbars-smooth
react-overlay-scrollbars-smooth
main

搜索帮助