首頁  >  文章  >  web前端  >  react怎麼實作捲軸

react怎麼實作捲軸

藏色散人
藏色散人原創
2022-12-20 11:59:143305瀏覽

react實作捲軸的方法:1、使用「render() {const translateDistancePercentage...}」方式設定捲軸滾動百分比;2、透過「.scrollBar {width: 362px;...} 」設定滾動條寬度;3、設定樣式為「left: -362px;top: 0px;position: absolute;」即可。

react怎麼實作捲軸

本教學操作環境:Windows10系統、react18版、Dell G3電腦。

react怎麼實作捲軸?

React-實作捲軸

一、實作效果

react怎麼實作捲軸

#二、實作程式碼

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;
      }
    }
}

下圖圈出來了一些關鍵樣式,顏色相同滴前後呼應喔~

react怎麼實作捲軸

推薦學習:《 react影片教學

以上是react怎麼實作捲軸的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn