首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue3.0仿el-scrollbar美化滚动条组件

Vue3.0仿el-scrollbar美化滚动条组件

原创
作者头像
andy2018
修改2021-01-08 15:14:20
2.8K0
修改2021-01-08 15:14:20
举报
文章被收录于专栏:h5h5

介绍

三天前有给大家分享一个Vue3 PC网页端自定义弹窗v3layer。这次带来的是全新开发的Vue3.0自定义模拟滚动条组件v3scroll。支持自定义滚动条大小、颜色、层叠及鼠标移开是否自动隐藏等功能。

Vue3-Scroll 一款使用vue3.x开发的类似饿了么模拟滚动条组件。开发灵感来自之前的vue2版本。

引入组件

import { createApp } from 'vue'
import App from './App.vue'
import './index.css'
 
// 引入滚动条组件v3scroll
import V3Scroll from './components/v3scroll'
 
createApp(App).use(V3Scroll).mount('#app')

支持水平+垂直滚动并存。

设置:native="true"显示默认系统滚动条。

快速使用

<v3-scroll>...</v3-scroll>裹住内容块即可快速生成一个模板滚动条组件。

<v3-scroll size="15" color="#f00" zIndex="1111">
    <p>显示自定义内容!</p>
</v3-scroll>
 
<v3-scroll @scroll="handleScroll">
    <p>显示自定义内容!</p>
</v3-scroll>

参数配置

props: {
    // 是否显示原生滚动条
    native: Boolean,
    // 是否自动隐藏滚动条
    autohide: Boolean,
    // 滚动条尺寸
    size: { type: [Number, String], default: '' },
    // 滚动条颜色
    color: String,
    // 滚动条层级
    zIndex: null
},
<template>
    <div class="vui__scrollbar" ref="ref__box" @mouseenter="handleMouseEnter" @mouseleave="handleMouseLeave" v-resize="handleResize">
        <div :class="['vscroll__wrap', {native: native}]" ref="ref__wrap" @scroll="handleScroll">
            <div class="vscroll__view" v-resize="handleResize">
                <slot />
            </div>
        </div>
        <div :class="['vscroll__bar vertical']" @mousedown="handleClickTrack($event, 0)" :style="{'width': parseInt(size)>=0 ? parseInt(size)+'px' : '', 'z-index': parseInt(zIndex)>=0 ? parseInt(zIndex) : ''}">
            <div class="vscroll__thumb" ref="ref__barY" :style="{'background': color, 'height': barHeight+'px'}" @mousedown="handleDragThumb($event, 0)"></div>
        </div>
        <div :class="['vscroll__bar horizontal']" @mousedown="handleClickTrack($event, 1)" :style="{'height': parseInt(size)>=0 ? parseInt(size)+'px' : '', 'z-index': parseInt(zIndex)>=0 ? parseInt(zIndex) : ''}">
            <div class="vscroll__thumb" ref="ref__barX" :style="{'background': color, 'width': barWidth+'px'}" @mousedown="handleDragThumb($event, 1)"></div>
        </div>
    </div>
</template>

/**
 * @Desc     vue3虚拟滚动条V3Scroll
 * @Time     andy by 2021-01
 * @About    Q:282310962
 */
<script>
    import { onMounted, ref, reactive, toRefs, nextTick } from 'vue'
    import domUtils from './utils/dom'
    export default {
        props: {
            // ...
        },
        
        /**
         * Vue3.x自定义指令写法
         */
        // 监听DOM尺寸变化
        directives: {
            'resize': {
                beforeMount: function(el, binding) {
                    let width = '', height = '';
                    function get() {
                        const elStyle = el.currentStyle ? el.currentStyle : document.defaultView.getComputedStyle(el, null);
                        if (width !== elStyle.width || height !== elStyle.height) {
                            binding.value({width, height});
                        }
                        width = elStyle.width;
                        height = elStyle.height;
                    }
                    el.__vueReize__ = setInterval(get, 16);
                },
                unmounted: function(el) {
                    clearInterval(el.__vueReize__);
                }
            }
        },
        setup(props, context) {
            const ref__box = ref(null)
            const ref__wrap = ref(null)
            const ref__barX = ref(null)
            const ref__barY = ref(null)
 
            const data = reactive({
                barWidth: 0,            // 滚动条宽度
                barHeight: 0,           // 滚动条高度
                ratioX: 1,              // 滚动条水平偏移率
                ratioY: 1,              // 滚动条垂直偏移率
                isTaped: false,         // 鼠标光标是否按住滚动条
                isHover: false,         // 鼠标光标是否悬停在滚动区
                isShow: !props.autohide, // 是否显示滚动条
            })
 
            onMounted(() => {
                nextTick(() => {
                    updated()
                })
            })
 
            // 鼠标滑入
            const handleMouseEnter = () => {
                data.isHover = true
                data.isShow = true
                updated()
            }
 
            // 鼠标滑出
            const handleMouseLeave = () => {
                data.isHover = false
                data.isShow = false
            }
 
            // 拖动滚动条
            const handleDragThumb = (e, index) => {
                const elWrap = ref__wrap.value
                const elBarX = ref__barX.value
                const elBarY = ref__barY.value
 
                data.isTaped = true
                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
                }
 
                // ...
            }
 
            // 点击滚动槽
            const handleClickTrack = (e, index) => {
                // ...
            }
 
            // 更新滚动区
            const updated = () => {
                if(props.native) return
                const elBox = ref__box.value
                const elWrap = ref__wrap.value
                const elBarX = ref__barX.value
                const elBarY = ref__barY.value
 
                let barSize = domUtils.getScrollBarSize()
 
                // 垂直滚动条
                if(elWrap.scrollHeight > elWrap.offsetHeight) {
                    data.barHeight = elBox.offsetHeight **2 / elWrap.scrollHeight
                    data.ratioY = (elWrap.scrollHeight - elBox.offsetHeight) / (elBox.offsetHeight - data.barHeight)
                    elBarY.style.transform = `translateY(${elWrap.scrollTop / data.ratioY}px)`
                    // 隐藏系统滚动条
                    if(barSize) {
                        elWrap.style.marginRight = -barSize + 'px'
                    }
                }else {
                    data.barHeight = 0
                    elBarY.style.transform = ''
                    elWrap.style.marginRight = ''
                }
 
                // 水平滚动条
                // ...
            }
 
            // 滚动区元素/DOM尺寸改变
            const handleResize = () => {
                // 执行更新操作
            }
 
            // ...
 
            return {
                ...toRefs(data),
                ref__box, ref__wrap, ref__barX, ref__barY,
 
                handleMouseEnter, handleMouseLeave,
                handleDragThumb, handleClickTrack,
                updated,
                
                // ...
            }
        }
    }
</script>

Okay,基于vue3.0开发自定义滚动条组件就介绍到这里。希望以上分享对大家有所帮助!💪🏻

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 引入组件
  • 快速使用
  • 参数配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档