午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

javascript實(shí)現(xiàn)自定義滾動(dòng)條效果

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

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

一個(gè)解決思路是:自定義滾動(dòng)條,隱藏原始滾動(dòng)條。

自定義滾動(dòng)條

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 表示拖動(dòng)滾動(dòng)條時(shí),鼠標(biāo)點(diǎn)擊的位置距離滾動(dòng)條左側(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); } /** * 這個(gè)函數(shù)處理聯(lián)動(dòng)(界面滾動(dòng)時(shí),觸發(fā)滾動(dòng)條滾動(dòng))這里的100是滾動(dòng)條的寬度 */ 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)按下,開(kāi)始拖動(dòng),設(shè)置當(dāng)前的位置為初始拖動(dòng)的位置 */ handleMouseDown = (e) => { this.setState({ isDraging: true, clickX: e.nativeEvent.offsetX, }); } /** * 當(dāng)鼠標(biāo)抬起時(shí),停止拖拽,設(shè)置當(dāng)前的點(diǎn)擊位置是0(這個(gè)有沒(méi)有必要設(shè)置) */ onMouseUp = () => { if (this.state.isDraging) { setTimeout(() => {this.setState({ isDraging: false, clickX: 0 }); }, 100); } } /** * 當(dāng)拖拽進(jìn)行時(shí)(鼠標(biāo)按下并開(kāi)始移動(dòng)),獲取當(dāng)前的位移,計(jì)算新的偏移量 * 注意:可以向右滾動(dòng),可以向左滾動(dòng) * 當(dāng)拖拽進(jìn)行時(shí),應(yīng)該計(jì)算出當(dāng)前的比例,然后Grid水平滾動(dòng) * 現(xiàn)在的問(wèn)題,如果鼠標(biāo)拖動(dòng)時(shí)移動(dòng)到滾動(dòng)條外部,那么無(wú)法觸發(fā)拖動(dòng) * */ onMouseMove = (e) => { e.persist(); if (this.state.isDraging) { // 新距離 = 原始距離 + (當(dāng)前滾動(dòng)的距離 - 初始滾動(dòng)的距離) let newX = this.state.x + e.nativeEvent.offsetX - this.state.clickX; newX = Math.min(newX, window.innerWidth - 100); // 最大的拖動(dòng)不能超過(guò)右側(cè)邊界 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;滾動(dòng)條樣式

對(duì)應(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;}/* 隱藏原始滾動(dòng)對(duì)象的滾動(dòng)條 */.react-demo::-webkit-scrollbar { width: 0;}滾動(dòng)條具體使用

具體使用,我們?cè)?Grid 中加入這個(gè)滾動(dòng)條

import ScrollBar from ’../components/scrollbar’;// Grid 原生滾動(dòng),觸發(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左右滾定時(shí),獲取當(dāng)前滾動(dòng)的比例(偏移量/全部寬度),并設(shè)置滾動(dòng)條進(jìn)行滾動(dòng) this.setScrollLeftRatio(scrollLeftRatio);}setScrollLeftRatio = (scrollLeftRatio) => { this.props.eventBus.dispatch(’set-scrollbar-left’, scrollLeftRatio);}// 在原始滾動(dòng)元素中,傳入eventBus,便于事件傳值處理// <ScrollBar eventBus={this.props.eventBus}/>

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 成人精品在线视频 | 伊人色区| 18精品爽国产三级网站 | 五月综合久久 | 日韩高清一区二区 | 亚洲精品卡一卡二 | 欧美色亚洲 | 日韩一区二区三区在线观看 | 欧美成人激情 | 激情高潮呻吟抽搐喷水 | 日韩香蕉网 | 91国产丝袜在线播放 | 久久综合中文字幕 | 亚洲14p| 亚洲玖玖爱 | 五月婷婷综合色 | 国产精品网站在线观看 | 国产乱淫a∨片免费观看 | 麻豆久久久久久久 | 色久综合网 | 国产精品一区二区三区在线 | 婷婷色一区二区三区 | 国产呦小j女精品视频 | 大地资源在线资源 | 新av在线 | 91最新国产 | 国产亚洲欧美视频 | 99re在线视频观看 | 久久草视频在线 | 成人手机av | 五月天激情国产综合婷婷婷 | 人人草网 | 在线播放一级片 | 国产无套精品一区二区 | 亚洲国产精品久久久久 | 黑人巨大精品欧美 | 4虎最新网址 | 三级视频在线看 | 男女拍拍拍网站 | 亚洲精品无吗 | 欧美成人精品欧美一级乱黄 |