国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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
相關文章:
主站蜘蛛池模板: 韩国一级黄色毛片 | 精品一区二区三区波多野结衣 | 韩国三级 mp4 | 亚洲精品不卡久久久久久 | 午夜不卡av免费 | 青青青青爽视频在线播放 | caoporen在线视频入口 | 欧美成人午夜在线全部免费 | 亚欧美| 女子张开腿让男人桶视频 | 国产页| 亚洲图片视频在线观看 | 中文字幕一区二区三区视频在线 | 久青草免费视频 | 成年女人免费观看 | 欧美一级毛片在线播放 | 香港a毛片免费全部播放 | 欧美成人亚洲综合精品欧美激情 | 国产精品久久一区一区 | 成人综合网址 | 国产精品美女一区二区三区 | 成人免费视频在 | 日本不卡一区二区三区在线观看 | 日本免费一区二区三区a区 日本免费一区二区三区看片 | 精品久久久久国产免费 | 国产精品网址 | 成年人免费网站在线观看 | 一色屋精品亚洲香蕉网站 | 午夜国产高清精品一区免费 | 黄色免费在线网址 | 亚洲加勒比久久88色综合1 | 影院成人区精品一区二区婷婷丽春院影视 | 亚洲免费色视频 | 一级片网址 | 亚洲天堂久久精品成人 | 国产精品久久久久毛片 | 日本国产在线视频 | 欧美大片在线播放 | 久久亚洲私人国产精品 | 亚洲成a人片在线观看精品 亚洲成a人片在线观看中 | 欧美成人a |