Home  >  Article  >  Web Front-end  >  How to implement scroll bar in react

How to implement scroll bar in react

藏色散人
藏色散人Original
2022-12-20 11:59:143367browse

How to implement scroll bars in react: 1. Use "render() {const translateDistancePercentage...}" to set the scroll bar scrolling percentage; 2. Use ".scrollBar {width: 362px;...} "Set the width of the scroll bar; 3. Set the style to "left: -362px; top: 0px; position: absolute;".

How to implement scroll bar in react

#The operating environment of this tutorial: Windows 10 system, react18 version, Dell G3 computer.

How to implement scroll bars in react?

React-implement scroll bar

1. Implementation effect

How to implement scroll bar in react

##2. Implementation code

jsx

render() {
const translateDistancePercentage = '33.33333333333333'; // 滚动条滚动百分比
console.log('滚动条滚动百分比' + translateDistancePercentage);
return (
// 滚动条
<div className="scrollBar" style={{ &#39;backgroundImage&#39;: `url(首页/滚动条外壳.png)` }}>
    <div className="scrollBarHousing">
        <span className="insideScrollBar" style={{ &#39;transform&#39;: `translateX(${translateDistancePercentage}%)`, &#39;backgroundImage&#39;: `url(首页/滚动条内里.png)` }}></span>
    </div>
</div>
)
}

less

 .scrollBar {
 /** 滚动条宽度 */
    width: 362px;
    height: 12px;
    left: 0px;
    top: 36px;
    position: absolute;
    opacity: 0.7;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    .scrollBarHousing {
      width: 100%;
      height: 100%;
      left: 0px;
      top: 0px;
      position: absolute;
      border-radius: 60px;
      /** 下面这个很关键喔 */
      overflow: hidden;
      .insideScrollBar {
        width: 100%;
        height: 100%;
        /** 让滚动条内里一来就先位于滚动条外最左侧 */
        left: -362px;
        top: 0px;
        position: absolute;
        border-radius: 60px;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
      }
    }
}

The picture below circles some key styles. The colors are the same and echo before and after~

How to implement scroll bar in react

Recommended study: "

react video tutorial

The above is the detailed content of How to implement scroll bar in react. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn