亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何拖動一個元素并確保光標不會比它快?

如何拖動一個元素并確保光標不會比它快?

守著星空守著你 2023-04-27 16:35:30
我有兩個拇指的滑塊。拇指可以沿著滑塊(線)一直移動,這可以通過增加或減少它們來實現margin-left,但是為了讓它們移動,狀態move必須是true,當每個拇指觸發事件時都會發生onClickDown。但是,如果事件onClickedUp被觸發,光標離開拇指或滑塊的區域,move設置為false,這會使拇指停止移動。沒關系,就是這個主意。問題是光標可能比拇指的移動速度更快,如下面的 gif 所示,是什么使光標離開拇指區域并設置為 false,即使這不是用戶想要的move。https://i.stack.imgur.com/hAXVP.gif 因此,為了使滑塊正常工作,用戶在移動拇指時必須格外小心,這是一個非常煩人的用戶體驗。簡而言之,我需要做的是確保光標不會比拇指移動得更快,無論我是否必須減慢光標或增加拇指的速度都沒有關系。我怎么能那樣做?這是我的代碼和一些注釋:import React, { Fragment } from 'react'import './Filter.css'const Filter = props => {    const sliderRef = React.useRef() // => main parent div    const initial_position = 0     const end_position = 200     const initial_min_value = 5 // => Initial price     const initial_max_value = 1290 // => Final price    let [thumb1_position, setValueThumb1] =  React.useState(0)    let [thumb2_position, setValueThumb2] =  React.useState(0)    let [min_value, setMinValue] =  React.useState(initial_min_value)    let [max_value, setMaxValue] =  React.useState(initial_max_value)    let [move, setMove] =  React.useState(false) // => Enable thumbs to move        // Ensure that the thumb_2 will be in the end of the slider at first    React.useEffect(() => {        setValueThumb2(sliderRef.current.offsetWidth - 5)    }, [])    // Here I get the position of the cursor within the element (slider) and move the thumbs based on it.    const handleChange = e => {        let thumb_class = e.target.className        var rect = sliderRef.current.getBoundingClientRect();        const current_position = e.clientX - rect.left; // X position within the element.
查看完整描述

1 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

我一直在挖掘并偶然發現了setPointerCapture()解決我的問題的方法。

它的功能是完全按照我對我的代碼所做的,但使用pointer events.?它使元素在 pointerdown 事件上移動并使其在 pointerup 事件上停止移動。

因為它幾乎完成了我需要做的所有事情,所以我可以擺脫我正在使用的一些功能。事實上,唯一保留的功能是handleChange.?除此之外,我還刪除了move狀態,但我必須添加一些refs才能獲取每個元素。

這是新代碼:

import React, { Fragment } from 'react'


import './Filter.css'



const Filter = props => {


? ? const sliderRef = React.useRef() // => main parent div


? ? const sliderRef = React.useRef() // => Div que engloba o slider

? ? const thumb_1_Ref = React.useRef() // => Div que engloba o slider

? ? const thumb_2_Ref = React.useRef() // => Div que engloba o slider

? ? const price_thumb_1_Ref = React.useRef() // => Div que engloba o slider

? ? const price_thumb_2_Ref = React.useRef() // => Div que engloba o slider

? ??

? ? const initial_position = 0?

? ??

? ? const initial_min_value = 5 // => Initial price?

? ? const initial_max_value = 1290 // => Final price


? ? let [thumb1_position, setValueThumb1] =? React.useState(0)

? ? let [thumb2_position, setValueThumb2] =? React.useState(0)

? ? let [mobile_thumb1_position, setValueMobileThumb1] =? React.useState(0)

? ? let [mobile_thumb2_position, setValueMobileThumb2] =? React.useState(0)

? ? let [min_value, setMinValue] =? React.useState(initial_min_value)

? ? let [max_value, setMaxValue] =? React.useState(initial_max_value)

? ??

? ? // Ensure that the thumb_2 will be in the end of the slider at first

? ? React.useEffect(() => {

? ? ? ? setValueThumb2(sliderRef.current.offsetWidth - 5)

? ? }, [])




? ? let slider

? ? let slider_price


? ? const beginSliding = e => {

? ? ? ? slider.onpointermove = slide

? ? ? ? slider.setPointerCapture(e.pointerId)

? ? }

? ??

? ? const stopSliding = e => {

? ? ? ? slider.onpointermove = null

? ? ? ? slider.releasePointerCapture(e.pointerId)

? ? }

? ??

? ? const slide = e => {

? ? ? ? const thumb_class = e.target.className


? ? ? ? let rect = sliderRef.current.getBoundingClientRect()

? ? ? ? let current_position = e.clientX - rect.left?

? ? ? ??

? ? ? ? if (thumb_class.includes('right-thumb')) {


? ? ? ? ? ? current_position = current_position - sliderRef.current.offsetWidth


? ? ? ? ? ? if (current_position >= initial_position) {

? ? ? ? ? ? ? ? current_position = initial_position

? ? ? ? ? ? }


? ? ? ? ? ? if (current_position <= mobile_thumb1_position - 175) {

? ? ? ? ? ? ? ? current_position = mobile_thumb1_position - 175

? ? ? ? ? ? }

? ? ? ? ? ??

? ? ? ? ? ? setValueMobileThumb2(current_position)

? ? ? ? }?

? ? ? ??

? ? ? ? if (thumb_class.includes('left-thumb')) {


? ? ? ? ? ? if (current_position <= initial_position) {

? ? ? ? ? ? ? ? current_position = initial_position

? ? ? ? ? ? }


? ? ? ? ? ? if (current_position >= mobile_thumb2_position + 175) {

? ? ? ? ? ? ? ? current_position = mobile_thumb2_position + 175

? ? ? ? ? ? }


? ? ? ? ? ? setValueMobileThumb1(current_position)

? ? ? ? }

? ? ? ??

? ? ? ? slider.style.transform = `translate(${current_position}px)`

? ? ? ? slider_price.style.transform = `translate(${current_position}px)`

? ? }

? ? ? ??

? ??

? ? const handleChange = e => {


? ? ? ? const thumb_class = e.target.className


? ? ? ? if (thumb_class.includes('left-thumb')) {


? ? ? ? ? ? slider = thumb_1_Ref.current;

? ? ? ? ? ? slider_price = price_thumb_1_Ref.current;


? ? ? ? ? ? slider.onpointerdown = beginSliding;

? ? ? ? ? ? slider.onpointerup = stopSliding;


? ? ? ? ? ? if (mobile_thumb1_position - initial_position < 1) {

? ? ? ? ? ? ? ? setMinValue(initial_min_value)

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? setMinValue((mobile_thumb1_position - initial_position) * 6.45)

? ? ? ? ? ? }


? ? ? ? } else if (thumb_class.includes('right-thumb')) {

? ? ? ? ? ??

? ? ? ? ? ? slider = thumb_2_Ref.current;

? ? ? ? ? ? slider_price = price_thumb_2_Ref.current;


? ? ? ? ? ? slider.onpointerdown = beginSliding;

? ? ? ? ? ? slider.onpointerup = stopSliding;


? ? ? ? ? ? if (mobile_thumb2_position > -1) {

? ? ? ? ? ? ? ? setMaxValue(initial_max_value)

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? setMaxValue((mobile_thumb2_position + 200) * 6.45)

? ? ? ? ? ? }

? ? ? ? }


? ? }




? ? return (

? ? ? ? <Fragment>

? ? ? ? ? ? <div>

? ? ? ? ? ? ? ? <h6 style={{marginBottom: '35px'}}>PRICE FILTER</h6>

? ? ? ? ? ? ? ? <div className="range-container"

? ? ? ? ? ? ? ? ? ? onMouseMove={(e) => handleChange(e)}

? ? ? ? ? ? ? ? ? ? ref={sliderRef}

? ? ? ? ? ? ? ? >

? ? ? ? ? ? ? ? ? ? <div className="range"


? ? ? ? ? ? ? ? ? ? >

? ? ? ? ? ? ? ? ? ? ? ? <span?

? ? ? ? ? ? ? ? ? ? ? ? ? ? className="rounded-circle left-thumb"

? ? ? ? ? ? ? ? ? ? ? ? ? ? style={{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? width:'15px',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? height: '15px',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? backgroundColor: 'red',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marginTop: '-6px',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft: thumb1_position - 7 + 'px'

? ? ? ? ? ? ? ? ? ? ? ? ? ? }}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ref={thumb_1_Ref}

? ? ? ? ? ? ? ? ? ? ? ? ></span>

? ? ? ? ? ? ? ? ? ? ? ? <span?

? ? ? ? ? ? ? ? ? ? ? ? ? ? className="rounded-circle right-thumb"

? ? ? ? ? ? ? ? ? ? ? ? ? ? style={{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? width:'15px',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? height: '15px',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? backgroundColor: 'black',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marginTop: '-6px',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft: thumb2_position - 7 + 'px'

? ? ? ? ? ? ? ? ? ? ? ? ? ? }}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ref={thumb_2_Ref}

? ? ? ? ? ? ? ? ? ? ? ? ></span>

? ? ? ? ? ? ? ? ? ? ? ? <p style={{

? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft: thumb1_position - 15 + 'px',

? ? ? ? ? ? ? ? ? ? ? ? ? ? position: 'absolute',

? ? ? ? ? ? ? ? ? ? ? ? ? ? marginTop: '15px'}}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ref={price_thumb_1_Ref}

? ? ? ? ? ? ? ? ? ? ? ? > {Math.floor(min_value)}

? ? ? ? ? ? ? ? ? ? ? ? </p>

? ? ? ? ? ? ? ? ? ? ? ? <p style={{

? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft: thumb2_position - 15 + 'px',

? ? ? ? ? ? ? ? ? ? ? ? ? ? position: 'absolute',

? ? ? ? ? ? ? ? ? ? ? ? ? ? marginTop: '15px'}}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ref={price_thumb_2_Ref}

? ? ? ? ? ? ? ? ? ? ? ? > {Math.floor(max_value)}

? ? ? ? ? ? ? ? ? ? ? ? </p>

? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? </div>

? ? ? ? </Fragment>

? ? )

}


export default Filter?



查看完整回答
反對 回復 2023-04-27
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號