色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

javascript實現自定義滾動條效果

瀏覽:2日期:2023-12-13 13:24:24

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

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

自定義滾動條

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 表示拖動滾動條時,鼠標點擊的位置距離滾動條左側的距離, 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); } /** * 這個函數處理聯動(界面滾動時,觸發滾動條滾動)這里的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, }); } /** * 當鼠標按下,開始拖動,設置當前的位置為初始拖動的位置 */ handleMouseDown = (e) => { this.setState({ isDraging: true, clickX: e.nativeEvent.offsetX, }); } /** * 當鼠標抬起時,停止拖拽,設置當前的點擊位置是0(這個有沒有必要設置) */ onMouseUp = () => { if (this.state.isDraging) { setTimeout(() => {this.setState({ isDraging: false, clickX: 0 }); }, 100); } } /** * 當拖拽進行時(鼠標按下并開始移動),獲取當前的位移,計算新的偏移量 * 注意:可以向右滾動,可以向左滾動 * 當拖拽進行時,應該計算出當前的比例,然后Grid水平滾動 * 現在的問題,如果鼠標拖動時移動到滾動條外部,那么無法觸發拖動 * */ onMouseMove = (e) => { e.persist(); if (this.state.isDraging) { // 新距離 = 原始距離 + (當前滾動的距離 - 初始滾動的距離) let newX = this.state.x + e.nativeEvent.offsetX - this.state.clickX; newX = Math.min(newX, window.innerWidth - 100); // 最大的拖動不能超過右側邊界 this.setState({ x: newX }); const leftRatio = newX / (window.innerWidth - 100); } } renderBottomToolbar = () => { return ( <divclassName='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 ( <divclassName='antiscroll-scrollbar antiscroll-scrollbar-vertical antiscroll-scrollbar-shown' ></div> ); } render() { return ( <div className='antiscroll-wrap'>{this.renderBottomToolbar()}{this.renderRightToolbar()} </div> ); }}ScrollBar.propTypes = propTypes;export default ScrollBar;滾動條樣式

對應的 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 原生滾動,觸發回調函數onScroll = () => { // todo: when clientWidth is smaller than innerWidth, don’t show bottom scrollBar let scrollLeftRatio = this._scrollLeft / (clientWidth - window.innerWidth); // 當原生DOM左右滾定時,獲取當前滾動的比例(偏移量/全部寬度),并設置滾動條進行滾動 this.setScrollLeftRatio(scrollLeftRatio);}setScrollLeftRatio = (scrollLeftRatio) => { this.props.eventBus.dispatch(’set-scrollbar-left’, scrollLeftRatio);}// 在原始滾動元素中,傳入eventBus,便于事件傳值處理// <ScrollBar eventBus={this.props.eventBus}/>

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 国产激情自拍 | 日本三级精品 | 一级特级aaa毛片 | 成人免费毛片网站 | 国产成人综合久久精品亚洲 | 国产在线精品一区二区中文 | 免费中文字幕 | 九九综合| 中文字幕成人免费高清在线 | 久久中文字幕免费视频 | 久久爽久久爽久久免费观看 | 一区二区三区在线观看免费 | 久在草视频 | 成人在线观看不卡 | 国模在线播放 | 99热成人精品热久久66 | 亚洲午夜精品一区二区 | 亚洲欧美日韩成人一区在线 | 黄色三级在线 | 欧美特黄特色aaa大片免费看 | 成人国产午夜在线视频 | 久久综合日韩亚洲精品色 | 亚洲精品美女在线观看播放 | 欧美一级一级片 | 在线日韩中文字幕 | 日本一本久道 | 黄色毛片a | 午夜在线视频一区二区三区 | 波多野结衣一区二区三区高清在线 | 中文字幕色站 | 日本高清专区一区二无线 | 国产精品久久久久久久久 | 999热成人精品国产免 | 久久高清一区二区三区 | 久久久久毛片免费观看 | 国产一区二区久久 | 日本在线观看不卡免费视频 | 全国男人的天堂天堂网 | jk制服福利在线播放 | 国产三区视频在线观看 | 欧美在线播放视频 |