说说react-custom-scrollbars插件在react hooks版本中的使用
react-custom-scrollbars插件
- 读前说明
- 基本使用
- 属性介绍
- onScroll
- renderView
- renderThumbVertical
- autoHide
- autoHideTimeout
读前说明
github地址:https://github.com/malte-wessel/react-custom-scrollbars
作用:
快速实现被包裹区域的滚动效果
关于本篇:
在官网以及其他文章中,都是介绍该插件在react class版本中的使用,
未涉及到react hooks版本中的使用方法。
所以本篇就介绍一下该插件在react hooks版本中的实现。
基本使用
安装插件:
npm install react-custom-scrollbars --save
在需要使用的文件中,引入插件:
import { Scrollbars } from 'react-custom-scrollbars';
需要让哪个元素实现滚动效果,就用<Scrollbars ></Scrollbars >
包裹住该元素即可。
写个demo:
import React from 'react'
import './scrollPlugin.css' // demo样式
import { Scrollbars } from 'react-custom-scrollbars'; // 引入插件组件// react hooks版本的组件
const ScrollPlugin = () => {return <div className='show-wrap'><ScrollbarsclassName='main' // 设置类名 方便在样式文件中写css代码><ul className='show-ul'> // 要滚动的区域{[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((item, index) => {return <li key={index}>{item}</li>})}</ul></Scrollbars></div>
}export default ScrollPlugin
实现的效果:
顺便贴一下demo中的css代码:
/* scrollPlugin.css */
.show-wrap {width: 300px;height: 400px;margin-left: 20px;margin-top: 20px;
}.main {width: 100%;height: 100%;overflow: hidden;background-color: pink;border-radius: 12px;
}.show-ul {list-style-type: none;width: 100%;margin: 0;padding: 0;
}.show-ul li {width: 100%;height: 60px;margin-top: 10px;background-color: cyan;border-radius: 12px;line-height: 60px;
}
属性介绍
官方md中给出了一堆属性,
但是没有注释说明。 有些陌生词汇,导致使用上可能不容易入手。
在这里从上到下,介绍一下常用的几个属性。
上面那些this.
后面的变量,都是函数,用的时候要写成函数的形式。
在react hooks版本中,没有this.
onScroll
滚动条滚动的时候,触发handleScroll
函数
demo:
import React from 'react'
import './scrollPlugin.css'
import { Scrollbars } from 'react-custom-scrollbars';const ScrollPlugin = () => {const handleScroll = () => { // 这里写滚动函数console.log('滚动条动了')}return <div className='show-wrap'><ScrollbarsclassName='main'onScroll={handleScroll} // 这里写入属性><ul className='show-ul'>{[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((item, index) => {return <li key={index}>{item}</li>})}</ul></Scrollbars></div>
}export default ScrollPlugin
滚动一下,可以看到控制台有输出内容:
renderView
这个函数是用来改变组件样式的。
样式可以在css文件写,这里就不多介绍了。
renderThumbVertical
改变滚动条样式。
写个demo:
import React from 'react'
import './scrollPlugin.css'
import { Scrollbars } from 'react-custom-scrollbars';const ScrollPlugin = () => {const renderThumb = () => { // renderThumb改变样式时被调用的函数,必须是函数const thumbStyle = { // 设置滚动条样式backgroundColor: 'rgb(9,164,215)',borderRadius: '4px'}return <div style={{...thumbStyle}} />}return <div className='show-wrap'><ScrollbarsclassName='main'renderThumbVertical={renderThumb} // 把新的滚动条样式写入组件><ul className='show-ul'>{[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((item, index) => {return <li key={index}>{item}</li>})}</ul></Scrollbars></div>
}export default ScrollPlugin
效果图:
autoHide
鼠标离开滚动区域后,滚动条会自动隐藏。
鼠标进入滚动区域后,滚动条会自动显示。
在插件组件中直接写入该属性即可使用:
<ScrollbarsclassName='main'autoHide // 自动隐藏属性>// 滚动内容
</Scrollbars>
autoHideTimeout
鼠标离开滚动区域多少ms后,滚动条才会自动消失。
该属性必须搭配autoHide属性,不然无效。
<ScrollbarsclassName='main'autoHideautoHideTimeout={1000} // 单位ms
>
over~
说说react-custom-scrollbars插件在react hooks版本中的使用相关推荐
- React Redux 进阶: Hooks 版本用法 Custom Context 局部 Store 实践
React Redux 进阶: Hooks 版本用法 & Custom Context 局部 Store 实践 文章目录 React Redux 进阶: Hooks 版本用法 & Cu ...
- React学习笔记5:React Hooks概述
文章目录 概述 React的两套API 类和函数的差异 副效应(副作用)是什么? 钩子(hook)出现的意义 三大框架对比 为什么学习hooks 应用场景 新版本特性解读 Hook使用规则 概述 Re ...
- react文字滚动插件_【赠书】Preact(React)核心原理详解
豆皮粉儿们,又见面了,今天这一期,由字节跳动数据平台的"winge(宝丁)",带大家见识见识前端"轮子"之一Preact框架. 提到Preact,你肯定会先想到 ...
- React Native 的未来与React Hooks
近期和一些朋友聊到了 React-Native 的官方重构状态,而刚好近期发布的 0.59.x 系列版本中,上层设计出现了比较大的调整,结合体验之后的状态,就想聊聊 React-Native 的现状. ...
- React 之 function 组件里使用 Hooks
目录 前言 一.react 内置的 Hook 1.useState 2.useEffect 二.自定义 Hook 三.使用 hook 遇到的问题 1.react 函数组件使用了 hook 后闪屏 前言 ...
- react 用html插件,React配置过程中用到的插件汇总
●react插件 ●react-dom插件 ●react-router插件 ●react-redux插件 ●babel插件 ●webpack插件 ●loader插件 ●jquery插件 ●moment ...
- React学习笔记6:React Hooks API总结
useState-保存状态(惰性初始化) 作用 函数组件添加状态 注意事项 初始化以及更新state 用来声明状态变量 使用步骤(使用useState来创建状态) 引入import React,{us ...
- React 2019年路线图发布!Hooks明年一季度上线
你可能在之前的一些文章和演讲中听过"Hooks"."Suspense"和"并发渲染"等新特性. 在这篇文章中,我们将介绍它们在React稳定 ...
- react开发插件-ES7 React/Redux/GraphQL/React-Native snippets
内容目录 ES7 React/Redux/GraphQL/React-Native snippets 插件 Basic Methods React React Native Redux PropTyp ...
- React + TypeScript实战(二)hooks用法
本文采用的react相关技术为: react@18.2.0 typescript@4.7.4 脚手架create-react-app 一.函数式组建的声明方式 import react, { FC } ...
最新文章
- 教师要与时俱进,不要自以为正确
- 洛谷 P1029 最大公约数和最小公倍数问题
- 服务器搭建php mysql5_Windows下php5+apache+mysql5 手工搭建笔记
- 基于keepalived实现多种模式的高可用集群网站架构
- 二叉树的中序遍历_94. 二叉树的中序遍历 - 中等
- Toggle Buttons(二)
- 5大网站汇总,搞定新颖的计算机专业毕业设计网站汇总
- 如何输入多组字符串c语言,求教大侠:如何输入一组字符串
- 利用Android Studio手动创建活动——笔记(超多图)
- 电科 | 电子器件微纳米加工技术
- 本人的月末结账步骤备忘
- 10步搞定App内测发布(蒲公英内测平台)
- Java实现吸血鬼数字
- 数据结构之病毒感染检测问题
- Kotlin 非对称加密RSA
- GlobalCash全球付万事达虚拟信用卡-可用于国外支付购物
- cmd导入python模块_Python如何导入模块
- unity小技巧收集总结--自用(一)
- 轮播图的实现,点击按钮切换轮播图等功能
- android:layout_alignParentLeft=true一下是什么意思
热门文章
- 中文转拼音【真正的完整版】 拼音 驼峰命名专用
- ArcGIS地图制图教程——超详细
- 要求用户首先输入员工数量,然后输入相应员工信息,格式为: name,age,gender,salary,hiredate 例如: 张三,25,男,5000,2006-02-15 每一行为一个员
- Kotlin入门(27)文件读写操作
- linux禁用137 138端口,如何关闭135、137、138、139、445端口方法
- 怎么自己制作返利机器人教程分享
- Redis(八):进阶篇 - 事务
- mac拷贝文件到u盘,mac拷贝文件到u盘很慢
- 「手绘控笔技巧」最有效的控笔练习,让你少走很多弯路
- 如何选择产品关键词?