当前位置:首页 > 技术分析 > 正文内容

轻量级 React.js 虚拟美化滚动条组件RScroll

ruisui882个月前 (04-08)技术分析14

前几天有给大家分享一个Vue自定义滚动条组件VScroll。今天再分享一个最新开发的React PC端模拟滚动条组件RScroll

vue+pc桌面端模拟滚动条组件VScroll

rscroll 一款基于react.js构建的超小巧自定义桌面端美化滚动条。支持原生滚动条、自动隐藏、滚动条尺寸/层级/颜色等功能。

如上图:支持垂直/水平滚动条。

功能及效果和之前VScroll保持一致。在开发支持参考借鉴了el-scrollbar等组件设计思想。

调用非常简单,只需包裹住内容即可快速生成一个漂亮的滚动条。

引入组件

// 引入滚动条组件RScroll
import RScroll from './components/rscroll'

快速使用


    
    

这里是内容信息!这里是内容信息!这里是内容信息!

这里是内容信息!这里是内容信息!这里是内容信息!

这里是内容信息!这里是内容信息!这里是内容信息!

这里是内容信息!这里是内容信息!这里是内容信息!

编码实现

在components目录下新建rscroll目录,并创建index.js页面。

rscroll滚动条模板

render() {
    return (
        
this.$ref__box = el} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
this.$ref__wrap = el} onScroll={this.handleScroll}>
{prop.children}
{/* 滚动条 */}
this.$ref__barY = el} onMouseDown={this.handleDragThumb.bind(this, 0)} style={{background: prop.color, height: opt.barHeight+'px'}}>
this.$ref__barX = el} onMouseDown={this.handleDragThumb.bind(this, 1)} style={{background: prop.color, width: opt.barWidth+'px'}}>
) }

react监听元素/DOM尺寸变化。由于react不像vue可以自定义指令directives,只能使用componentDidUpdate来监听。

componentDidUpdate(preProps, preState) {
    // 监听内层view DOM尺寸变化
    let $view = this.$ref__wrap.querySelector('.vscroll__view')
    const viewStyle = $view.currentStyle ? $view.currentStyle : document.defaultView.getComputedStyle($view, null);
    if(preState.$viewWidth !== viewStyle.width || preState.$viewHeight !== viewStyle.height) {
        this.setState({ $viewWidth: viewStyle.width, $viewHeight: viewStyle.height })
        this.updated()
    }
}
/**
 * ReactJs|Next.js自定义滚动条组件RScroll
 */
import React from 'react'

class RScroll extends React.Component {
    /**
     * 默认配置
     */
    static defaultProps = {
        // 是否显示原生滚动条
        native: false,
        // 鼠标滑出是否自动隐藏滚动条
        autohide: false,
        // 自定义滚动条大小
        size: '',
        // 自定义滚动条颜色
        color: '',
        // 滚动条层级
        zIndex: null
    }

    constructor(props) {
        super(props)
        this.state = {
            barWidth: 0,                    // 滚动条宽度
            barHeight: 0,                   // 滚动条高度
            ratioX: 1,                      // 滚动条水平偏移率
            ratioY: 1,                      // 滚动条垂直偏移率
            isTaped: false,                 // 鼠标光标是否按住滚动条
            isHover: false,                 // 鼠标光标是否悬停在滚动区
            isShow: !this.props.autohide,   // 是否显示滚动条

            $viewWidth: null,
            $viewHeight: null,
        }
    }

    // 鼠标滑入
    handleMouseEnter = () => {
        this.setState({
            isHover: true, isShow: true
        })
        this.updated()
    }

    // 鼠标滑出
    handleMouseLeave = () => {
        const { isTaped } = this.state
        this.setState({ isHover: false })
        this.setState({ isShow: false })
    }

    // 拖动滚动条
    handleDragThumb = (index, e) => {
        let _this = this
        this.setState({ isTaped: true })

        const { ratioX, ratioY } = this.state
        let c = {}
        // 阻止默认事件
        domUtils.isIE() ? (e.returnValue = false, e.cancelBubble = true) : (e.stopPropagation(), e.preventDefault())
        document.onselectstart = () => false

        if(index == 0) {
            c.dragY = true
            c.clientY = e.clientY
        }else {
            c.dragX = true
            c.clientX = e.clientX
        }

        domUtils.on(document, 'mousemove', function(evt) {
            if(_this.state.isTaped) {
                if(c.dragY) {
                    _this.$ref__wrap.scrollTop += (evt.clientY - c.clientY) * ratioY
                    _this.$ref__barY.style.transform = `translateY(${_this.$ref__wrap.scrollTop / ratioY}px)`
                }
                if(c.dragX) {
                    _this.$ref__wrap.scrollLeft += (evt.clientX - c.clientX) * ratioX
                    _this.$ref__barX.style.transform = `translateX(${_this.$ref__wrap.scrollLeft / ratioX})`
                }
            }
        })
        domUtils.on(document, 'mouseup', function() {
            _this.setState({ isTaped: false })
            document.onmouseup = null
            document.onselectstart = null
            if(!_this.state.isHover && _this.props.autohide) {
                _this.setState({ isShow: false })
            }
        })
    }

    // 点击滚动槽
    handleClickTrack = (index, e) => {
        // ...
    }

    // 更新滚动区
    updated = () => {
        if(this.props.native) return
        let c = {}
        let barSize = domUtils.getScrollBarSize()

        // 垂直滚动条
        if(this.$ref__wrap.scrollHeight > this.$ref__wrap.offsetHeight) {
            c.barHeight = this.$ref__box.offsetHeight **2 / this.$ref__wrap.scrollHeight
            c.ratioY = (this.$ref__wrap.scrollHeight - this.$ref__box.offsetHeight) / (this.$ref__box.offsetHeight - c.barHeight)
            this.$ref__barY.style.transform = `translateY(${this.$ref__wrap.scrollTop / c.ratioY}px)`
            // 隐藏系统滚动条
            if(barSize) {
                this.$ref__wrap.style.marginRight = -barSize + 'px'
            }
        }else {
            c.barHeight = 0
            this.$ref__barY.style.transform = ''
            this.$ref__wrap.style.marginRight = ''
        }

        // 水平滚动条
        ...
    }

    // 鼠标滚动
    handleScroll = (e) => {
        const { onScroll } = this.props
        typeof onScroll === 'function' && onScroll.call(this, e)
        this.updated()
    }

    render() {
        return (
            // ...
        )
    }
}

export default RScroll


    
    

这里是内容信息!这里是内容信息!这里是内容信息!

// 监听滚动事件 handleScroll = (e) => { let _scrollTop = e.target.scrollTop let _scrollStatus // 判断滚动状态 if(e.target.scrollTop == 0) { _scrollStatus = '滚到至顶部' } else if(e.target.scrollTop + e.target.offsetHeight >= e.target.scrollHeight) { _scrollStatus = '滚动至底部' }else { _scrollStatus = '正滚动中..' } this.setState({ scrollTop: _scrollTop, scrollStatus: _scrollStatus }) }

好了,以上就是基于react.js开发自定义美化滚动条组件。希望大家能喜欢~~

扫描二维码推送至手机访问。

版权声明:本文由ruisui88发布,如需转载请注明出处。

本文链接:http://www.ruisui88.com/post/3361.html

标签: react-query
分享给朋友:

“轻量级 React.js 虚拟美化滚动条组件RScroll” 的相关文章

vue项目-父页面数据变化使子页面更新的几种情况

当操作页面时候,特别是增删改操作之后,数据会有所改变,这个时候我们希望组件中的数据要和最新数据一致,就需要重新更新渲染。以下是针对几种不同情况下方式:一.子页面调用接口后重新渲染1.使用ref方式父组件中用ref=“xxx” 来声明子组件,然后通过在父组件值改变的地方来调用子组件中的方法this.$...

Git分布式系统---Gitlab多人工作流程

前言在上一次推文中,我们已经很清楚的讲解了如何创建本地仓库、提交(push)项目到远程仓库以及从远程仓库clone(克隆)项目到本地的相关操作。大家可以先去看前面的推文(快速掌握Git分布式系统操作)点击查看目前无论你是否步入社会还是在校学生,都会使用Gitlab来进行团队的代码管理。(可以这样说:...

用IDEA开发如何用Git快速拉取指定分支代码?

1,准备空的文件夹,git init2,关联远程仓库,git remote add origin gitlab地址3,拉取远程分支代码,git pull origin 远程分支名再用IDEA打开项目即可...

7 招教你轻松搭建以图搜图系统

作者 | 小龙责编 | 胡巍巍当您听到“以图搜图”时,是否首先想到了百度、Google 等搜索引擎的以图搜图功能呢?事实上,您完全可以搭建一个属于自己的以图搜图系统:自己建立图片库;自己选择一张图片到库中进行搜索,并得到与其相似的若干图片。Milvus 作为一款针对海量特征向量的相似性检索引擎,旨在...

「云原生」Containerd ctr,crictl 和 nerdctl 命令介绍与实战操作

一、概述作为接替Docker运行时的Containerd在早在Kubernetes1.7时就能直接与Kubelet集成使用,只是大部分时候我们因熟悉Docker,在部署集群时采用了默认的dockershim。在V1.24起的版本的kubelet就彻底移除了dockershim,改为默认使用Conta...

Gemini应用在Android上广泛推出2.0闪电模式切换器

#头条精品计划# 快速导读谷歌(搜索)应用的测试频道在安卓设备的双子应用中推出了2.0闪电实验功能,现已向稳定用户开放。双子应用通过谷歌应用运行,目前推出的15.50版本中,用户可通过模型选择器体验不同选项,包括1.5专业版、1.5闪电版和2.0闪电实验版。2.0闪电实验模型提供了更快的响应速度和优...