简体   繁体   English

如何用react js模拟长按?

[英]How to simulate long press with react js?

I want to trigger long press event with click event.我想用点击事件触发长按事件。 is there any way to that in react js?在 React js 中有什么办法吗?

something close to this, is the jQuery trigger() function. but i want something like trigger("longPress") or open up right click menu with left click in react.接近于此的是 jQuery trigger() function。但我想要 trigger("longPress") 之类的东西,或者用左键单击打开右键单击菜单以做出反应。 both mentioned (long press trigger / open up right click menu) are ideal for me两者都提到(长按触发器/打开右键单击菜单)对我来说是理想的

you can do this hack by get hold time你可以通过保持时间来做这个黑客

https://stackblitz.com/edit/react-d1txdm https://stackblitz.com/edit/react-d1txdm

export default function App() {
  let triggerTime;
  return (
    <div>
      <h1>Try on Google Chrome Desktop</h1>
      <p>Open the console log to see how the event gets triggered.</p>
      <p>The event should not get triggered if there is a long click.</p>
      <img
        src="https://via.placeholder.com/200.png/09f/fff"
        onClick={(e) => {
          if (triggerTime > 1000) return;
          else console.log('normal click');
        }}
        onMouseDown={() => {
          triggerTime = new Date().getTime();
        }}
        onMouseUp={() => {
          let thisMoment = new Date().getTime();
          triggerTime = thisMoment - triggerTime;
        }}
      />
    </div>
  );
}

What about something like this:像这样的东西怎么样:

const myComponent = () => {

    let clickHoldTimer = null;

    const handleMouseDown = () => {
        clickHoldTimer = setTimeout(() => {
            //Action to be performed after holding down mouse
        }, 1000); //Change 1000 to number of milliseconds required for mouse hold
    }

    const handleMouseUp = () => {
        clearTimeout(clickHoldTimer);
    }

    return (
        <div onMouseDown={handleMouseDown} onMouseUp={handleMouseUp} />
    )

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM