最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript實現(xiàn)自定義滾動條效果

 更新時間:2021年08月19日 10:51:16   作者:Michael18811380328  
這篇文章主要為大家詳細(xì)介紹了javascript實現(xiàn)自定義滾動條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

在實際項目中,遇到上下滾動條和左右滾動條不在一個DIV內(nèi)部,所以某些情況下,右側(cè)滾動條不可見。但是需要咋同一個視口內(nèi)顯示兩個滾動條。

一個解決思路是:自定義滾動條,隱藏原始滾動條。

自定義滾動條

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 表示拖動滾動條時,鼠標(biāo)點擊的位置距離滾動條左側(cè)的距離, 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);
  }

  /**
   * 這個函數(shù)處理聯(lián)動(界面滾動時,觸發(fā)滾動條滾動)這里的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,
    });
  }

  /**
   * 當(dāng)鼠標(biāo)按下,開始拖動,設(shè)置當(dāng)前的位置為初始拖動的位置
   */
  handleMouseDown = (e) => {
    this.setState({
      isDraging: true,
      clickX: e.nativeEvent.offsetX,
    });
  }

  /**
   * 當(dāng)鼠標(biāo)抬起時,停止拖拽,設(shè)置當(dāng)前的點擊位置是0(這個有沒有必要設(shè)置)
   */
  onMouseUp = () => {
    if (this.state.isDraging) {
      setTimeout(() => {
        this.setState({ isDraging: false, clickX: 0 });
      }, 100);
    }
  }

  /**
   * 當(dāng)拖拽進(jìn)行時(鼠標(biāo)按下并開始移動),獲取當(dāng)前的位移,計算新的偏移量
   * 注意:可以向右滾動,可以向左滾動
   * 當(dāng)拖拽進(jìn)行時,應(yīng)該計算出當(dāng)前的比例,然后Grid水平滾動
   * 現(xiàn)在的問題,如果鼠標(biāo)拖動時移動到滾動條外部,那么無法觸發(fā)拖動
   * */ 
  onMouseMove = (e) => {
    e.persist();
    if (this.state.isDraging) {
      // 新距離 = 原始距離 + (當(dāng)前滾動的距離 - 初始滾動的距離)
      let newX = this.state.x + e.nativeEvent.offsetX - this.state.clickX;
      newX = Math.min(newX, window.innerWidth - 100); // 最大的拖動不能超過右側(cè)邊界
      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;

滾動條樣式

對應(yīng)的 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 原生滾動,觸發(fā)回調(diào)函數(shù)
onScroll = () => {
  // todo: when clientWidth is smaller than innerWidth, don't show bottom scrollBar
  let scrollLeftRatio = this._scrollLeft / (clientWidth - window.innerWidth);
  // 當(dāng)原生DOM左右滾定時,獲取當(dāng)前滾動的比例(偏移量/全部寬度),并設(shè)置滾動條進(jìn)行滾動
  this.setScrollLeftRatio(scrollLeftRatio);
}

setScrollLeftRatio = (scrollLeftRatio) => {
  this.props.eventBus.dispatch('set-scrollbar-left', scrollLeftRatio);
}

// 在原始滾動元素中,傳入eventBus,便于事件傳值處理
// <ScrollBar eventBus={this.props.eventBus}/>

自定義滾動條也有很多開源第三方組件,我們優(yōu)先使用第三方庫實現(xiàn)(處理滾動條計算考慮情況較多)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • javascript中數(shù)組與對象的使用方法區(qū)別

    javascript中數(shù)組與對象的使用方法區(qū)別

    數(shù)組(array)是按次序排列的一組值。JS其實沒有真正的數(shù)組,只是用對象模擬數(shù)組。本質(zhì)上,數(shù)組屬于一種特殊的對象。typeof運算符會返回數(shù)組的類型是object。在javascript中,數(shù)組又可以認(rèn)為是索引數(shù)組,即可以用整數(shù)來進(jìn)行索引。數(shù)組和對象在這種情況下非常接近。
    2022-12-12
  • 怎樣用JavaScript實現(xiàn)原型模式

    怎樣用JavaScript實現(xiàn)原型模式

    這篇文章主要介紹了怎樣用JavaScript實現(xiàn)原型模式,想學(xué)習(xí)設(shè)計模式的同學(xué),可以參考下
    2021-04-04
  • Javascript中的async awai的用法

    Javascript中的async awai的用法

    本篇文章主要介紹了Javascript中的async/awai的用法,將分享async / await是如何工作的,有興趣的可以了解一下
    2017-05-05
  • javascript實現(xiàn)顯示和隱藏div方法匯總

    javascript實現(xiàn)顯示和隱藏div方法匯總

    本文章通過幾個簡單的實例告訴你如何來實例關(guān)于隱藏與顯示div層及關(guān)閉層與隱藏的區(qū)別分析哦,有需要的同學(xué)可以參考一下本文章。
    2015-08-08
  • iScroll中事件點擊觸發(fā)兩次解決方案

    iScroll中事件點擊觸發(fā)兩次解決方案

    iScroll是我們在做手機(jī)網(wǎng)頁中常用的滑動控件之一。單說其功能已相當(dāng)豐富。但個別時候也是會掉坑的,正好這次就遇上了。在android的app中嵌入網(wǎng)頁時不少手機(jī)會出現(xiàn)一次點擊兩次觸發(fā)的現(xiàn)象。經(jīng)過一段時間的折騰,總算想到了一個還算合理的解決放案。
    2015-03-03
  • webpack3升級到webpack4遇到問題總結(jié)

    webpack3升級到webpack4遇到問題總結(jié)

    這篇文章主要介紹了webpack3升級到webpack4遇到問題總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • js表單登陸驗證示例

    js表單登陸驗證示例

    這篇文章主要介紹了js表單登陸驗證的方法,基于thinkPHP前端頁面實現(xiàn)javascript針對表單用戶名與密碼的驗證功能,需要的朋友可以參考下
    2016-10-10
  • javascript+css實現(xiàn)進(jìn)度條效果

    javascript+css實現(xiàn)進(jìn)度條效果

    這篇文章主要為大家詳細(xì)介紹了javascript+css實現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • js實現(xiàn)圖片懶加載效果

    js實現(xiàn)圖片懶加載效果

    這篇文章主要為大家詳細(xì)介紹了js實現(xiàn)圖片懶加載效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • JavaScript中隨機(jī)數(shù)方法?Math.random()

    JavaScript中隨機(jī)數(shù)方法?Math.random()

    這篇文章主要介紹了JavaScript中隨機(jī)數(shù)方法?Math.random(),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06

最新評論

太和县| 浦城县| 乌审旗| 曲松县| 宜城市| 麟游县| 中超| 临沂市| 错那县| 莫力| 拜泉县| 垫江县| 东乡族自治县| 南丹县| 石渠县| 开阳县| 边坝县| 额敏县| 张北县| 台北县| 英吉沙县| 睢宁县| 绥棱县| 邳州市| 永年县| 绥德县| 盐池县| 丽江市| 清丰县| 德惠市| 南投市| 宝兴县| 舒城县| 紫阳县| 双牌县| 天气| 黑水县| 桦川县| 防城港市| 通河县| 石河子市|