In actual projects, the up and down scroll bars and the left and right scroll bars are not inside a DIV, so in some cases, the right scroll bar is not visible. But we need to display two scroll bars in the same viewport. One solution is to customize the scroll bar and hide the original scroll bar. Custom scroll barsscrollbar.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 indicates the distance from the left side of the scroll bar to the mouse click position when dragging the scroll bar, 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); } /** * This function handles the linkage (when the interface scrolls, it triggers the scroll bar to scroll). Here 100 is the width of the scroll bar*/ setScrollBarLeft = (leftRatio) => { // when bottom scrollbar is dragging, can't set scrollBar left if (this.state.isDraging) return; this.setState({ x: (window.innerWidth - 100) * leftRatio, }); } /** * When the mouse is pressed, start dragging and set the current position to the initial drag position*/ handleMouseDown = (e) => { this.setState({ isDraging: true, clickX: e.nativeEvent.offsetX, }); } /** * When the mouse is lifted, stop dragging and set the current click position to 0 (is this necessary?) */ onMouseUp = () => { if (this.state.isDraging) { setTimeout(() => { this.setState({ isDraging: false, clickX: 0 }); }, 100); } } /** * When dragging (mouse is pressed and starts to move), get the current displacement and calculate the new offset * Note: you can scroll to the right and scroll to the left * When dragging, the current ratio should be calculated, and then the Grid scrolls horizontally * The current problem is that if the mouse moves outside the scroll bar while dragging, the dragging cannot be triggered * */ onMouseMove = (e) => { e.persist(); if (this.state.isDraging) { // New distance = original distance + (current scrolling distance - initial scrolling distance) let newX = this.state.x + e.nativeEvent.offsetX - this.state.clickX; newX = Math.min(newX, window.innerWidth - 100); // The maximum drag cannot exceed the right boundary this.setState({ x: newX }); const leftRatio = newX / (window.innerWidth - 100); } } renderBottomToolbar = () => { return ( <div className="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 ( <div className="antiscroll-scrollbar antiscroll-scrollbar-vertical antiscroll-scrollbar-shown" ></div> ); } render() { return ( <div id="scrollOverlay" className="antiscroll-wrap"> {this.renderBottomToolbar()} {this.renderRightToolbar()} </div> ); } } ScrollBar.propTypes = propTypes; export default ScrollBar; Scrollbar StyleCorresponding 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; } /* Hide the scroll bar of the original scroll object */ .react-demo::-webkit-scrollbar { width: 0; } Scroll bar specific useFor specific use, we add this scroll bar to the Grid import ScrollBar from '../components/scrollbar'; // Grid native scrolling, triggering callback function onScroll = () => { // todo: when clientWidth is smaller than innerWidth, don't show bottom scrollBar let scrollLeftRatio = this._scrollLeft / (clientWidth - window.innerWidth); // When the native DOM scrolls left or right, get the current scroll ratio (offset/total width) and set the scroll bar to scroll this.setScrollLeftRatio(scrollLeftRatio); } setScrollLeftRatio = (scrollLeftRatio) => { this.props.eventBus.dispatch('set-scrollbar-left', scrollLeftRatio); } // In the original scroll element, pass in eventBus to facilitate event value processing // <ScrollBar eventBus={this.props.eventBus}/> There are also many open source third-party components for custom scroll bars. We prefer to use third-party libraries to implement them (there are many considerations for scroll bar calculations). The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: A brief introduction to Linux environment variable files
>>: Detailed explanation of the principle and usage of cursor (DECLARE) in MySQL stored procedure
Table of contents A pitfall about fileReader File...
Use the Linux utility certbot to generate https c...
Learned ConcurrentHashMap but don’t know how to a...
What is ELK? ELK is a complete set of log collect...
I encountered a problem today. When entering the ...
Table of contents Horizontal bar chart Dynamicall...
During development, a good user interface will al...
Prototype chain inheritance Prototype inheritance...
In this section, the author describes the special...
How does "adaptive web design" work? It’...
I think everyone often worries about finding pict...
1. The difference between Http and Https HTTP: It...
This article uses an example to describe how to u...
background I talked to my classmates about a boun...
1. What is a servlet 1.1. Explain in official wor...