JavaScript to achieve custom scroll bar effect

JavaScript to achieve custom scroll bar effect

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 bars

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 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 Style

Corresponding 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 use

For 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:
  • Detailed explanation of scroll bar events using javascript
  • Native JS to achieve custom scroll bar effect
  • JS custom scroll bar effect simple implementation code
  • JavaScript limited range drag and custom scroll bar application (3)
  • js scroll bar smooth movement example code
  • Learn to use the jQuery iScroll.js mobile scroll bar plugin
  • js/jquery obtains the browser window visible area height and width and scroll bar height implementation code
  • JS code to determine the bottom of the scroll bar
  • JS implements the method of judging that the scroll bar has scrolled to the bottom of the page and executing the event
  • js operation scroll bar event example

<<:  A brief introduction to Linux environment variable files

>>:  Detailed explanation of the principle and usage of cursor (DECLARE) in MySQL stored procedure

Recommend

A pitfall and solution of using fileReader

Table of contents A pitfall about fileReader File...

An example of how Tomcat manages Session

Learned ConcurrentHashMap but don’t know how to a...

How to install ELK in Docker and implement JSON format log analysis

What is ELK? ELK is a complete set of log collect...

Solve the problem of case sensitivity of Linux+Apache server URL

I encountered a problem today. When entering the ...

Echarts Bar horizontal bar chart example code

Table of contents Horizontal bar chart Dynamicall...

CSS animation property usage and example code (transition/transform/animation)

During development, a good user interface will al...

Detailed explanation of 6 ways of js inheritance

Prototype chain inheritance Prototype inheritance...

Web page HTML code: production of scrolling text

In this section, the author describes the special...

CSS code to control the background color of the web page

I think everyone often worries about finding pict...

Implementation of Nginx configuration Https security authentication

1. The difference between Http and Https HTTP: It...

mysql group_concat method example to write group fields into one row

This article uses an example to describe how to u...

MySQL InnoDB row_id boundary overflow verification method steps

background I talked to my classmates about a boun...

In-depth understanding of the creation and implementation of servlets in tomcat

1. What is a servlet 1.1. Explain in official wor...