javascript實現自定義滾動條效果
在實際項目中,遇到上下滾動條和左右滾動條不在一個DIV內部,所以某些情況下,右側滾動條不可見。但是需要咋同一個視口內顯示兩個滾動條。
一個解決思路是:自定義滾動條,隱藏原始滾動條。
自定義滾動條scrollbar.js
import React, { Component } from ’react’;import PropTypes from ’prop-types’;import ’../css/scrollbar.css’;const propTypes = { eventBus: PropTypes.object.isRequired,};class ScrollBar extends Component { constructor(props) { super(props); this.state = { isDraging: false, // X: bottom scrollbar offset left, range [0, innerWidth - 100]. When dragging, x is changing x: null, // clickX 表示拖動滾動條時,鼠標點擊的位置距離滾動條左側的距離, range [0, 100], When dragging, clickX isn’t changing clickX: 0, }; } componentDidMount() { this.unsubscribeScrollToColumn = this.props.eventBus.subscribe(’set-scrollbar-left’, this.setScrollBarLeft); document.addEventListener(’mouseup’, this.onMouseUp); } componentWillUnmount() { this.unsubscribeScrollToColumn(); document.removeEventListener(’mouseup’, this.onMouseUp); } /** * 這個函數處理聯動(界面滾動時,觸發滾動條滾動)這里的100是滾動條的寬度 */ setScrollBarLeft = (leftRatio) => { // when bottom scrollbar is dragging, can’t set scrollBa left if (this.state.isDraging) return; this.setState({ x: (window.innerWidth - 100) * leftRatio, }); } /** * 當鼠標按下,開始拖動,設置當前的位置為初始拖動的位置 */ handleMouseDown = (e) => { this.setState({ isDraging: true, clickX: e.nativeEvent.offsetX, }); } /** * 當鼠標抬起時,停止拖拽,設置當前的點擊位置是0(這個有沒有必要設置) */ onMouseUp = () => { if (this.state.isDraging) { setTimeout(() => {this.setState({ isDraging: false, clickX: 0 }); }, 100); } } /** * 當拖拽進行時(鼠標按下并開始移動),獲取當前的位移,計算新的偏移量 * 注意:可以向右滾動,可以向左滾動 * 當拖拽進行時,應該計算出當前的比例,然后Grid水平滾動 * 現在的問題,如果鼠標拖動時移動到滾動條外部,那么無法觸發拖動 * */ onMouseMove = (e) => { e.persist(); if (this.state.isDraging) { // 新距離 = 原始距離 + (當前滾動的距離 - 初始滾動的距離) let newX = this.state.x + e.nativeEvent.offsetX - this.state.clickX; newX = Math.min(newX, window.innerWidth - 100); // 最大的拖動不能超過右側邊界 this.setState({ x: newX }); const leftRatio = newX / (window.innerWidth - 100); } } renderBottomToolbar = () => { return ( <divclassName='antiscroll-scrollbar antiscroll-scrollbar-horizontal antiscroll-scrollbar-shown'style={{transform: `translateX(${this.state.x}px)`}}draggable='true'onMouseDown={this.handleMouseDown}onMouseMove={this.onMouseMove}onMouseUp={this.onMouseUp} ></div> ); } // todo: rightToolbar event handle renderRightToolbar = () => { return ( <divclassName='antiscroll-scrollbar antiscroll-scrollbar-vertical antiscroll-scrollbar-shown' ></div> ); } render() { return ( <div className='antiscroll-wrap'>{this.renderBottomToolbar()}{this.renderRightToolbar()} </div> ); }}ScrollBar.propTypes = propTypes;export default ScrollBar;滾動條樣式
對應的 scrollbar.css
#scrollOverlay { display: inline-block; overflow: hidden; position: fixed; left: 0; right: 0; top: 156px; bottom: 0; z-index: 4; pointer-events: none; opacity: .7;}#scrollOverlay .antiscroll-scrollbar { pointer-events: auto; z-index: 2; background-color: hsla(0,0%,0%,0.28); box-shadow: inset 0 0 0 1px hsl(0,0%,100%); border-radius: 5px;}#scrollOverlay .antiscroll-scrollbar-horizontal { height: 12px; width: 100px; position: absolute; bottom: 32px;}#scrollOverlay .antiscroll-scrollbar-vertical { width: 12px; height: 100px; position: absolute; right: 0;}/* 隱藏原始滾動對象的滾動條 */.react-demo::-webkit-scrollbar { width: 0;}滾動條具體使用
具體使用,我們在 Grid 中加入這個滾動條
import ScrollBar from ’../components/scrollbar’;// Grid 原生滾動,觸發回調函數onScroll = () => { // todo: when clientWidth is smaller than innerWidth, don’t show bottom scrollBar let scrollLeftRatio = this._scrollLeft / (clientWidth - window.innerWidth); // 當原生DOM左右滾定時,獲取當前滾動的比例(偏移量/全部寬度),并設置滾動條進行滾動 this.setScrollLeftRatio(scrollLeftRatio);}setScrollLeftRatio = (scrollLeftRatio) => { this.props.eventBus.dispatch(’set-scrollbar-left’, scrollLeftRatio);}// 在原始滾動元素中,傳入eventBus,便于事件傳值處理// <ScrollBar eventBus={this.props.eventBus}/>
自定義滾動條也有很多開源第三方組件,我們優先使用第三方庫實現(處理滾動條計算考慮情況較多)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
