简体   繁体   English

当我完成拖动并且鼠标上升时,如何使我的可拖动 div 不运行 onClick function?

[英]How to make my draggable div not run the onClick function when I finish dragging and my mouse goes up?

So I have a menu div that is draggable.所以我有一个可拖动的菜单 div。 Right now it's working almost perfectly, except when my mouse goes up, it sets off the "onClick" function which triggers a modal to pop up.现在它工作得几乎完美,除了当我的鼠标上升时,它会触发“onClick” function 触发模式弹出。 I don't want that to happen.我不希望这种情况发生。 I want that modal to pop up ONLY on click, not after dragging.我希望该模式仅在单击时弹出,而不是在拖动后弹出。 But when my mouse goes up after dragging, I guess it is being triggered as a click event.但是当我的鼠标在拖动后上升时,我猜它是作为点击事件触发的。

jsfiddle: https://jsfiddle.net/annahisenberg/ft10ersb/1/ jsfiddle: https://jsfiddle.net/annahisenberg/ft10ersb/1/

Code:代码:

constructor(props) {
    super(props);
    this.state = {
      x: this.props.x,
      y: this.props.y,
      showMoreOptionsPopup: false,
      showHelpModal: false
    };

    this.reff = React.createRef();
  }

  componentDidMount() {
    this.pos1 = 0;
    this.pos2 = 0;
    this.pos3 = 0;
    this.pos4 = 0;
  }

  dragMouseDown = e => {
    e.preventDefault();
    this.pos3 = e.clientX;
    this.pos4 = e.clientY;
    document.onmouseup = this.closeDragElement;
    document.onmousemove = this.elementDrag;
  };

  elementDrag = e => {
    e.preventDefault();
    this.pos1 = this.pos3 - e.clientX;
    this.pos2 = this.pos4 - e.clientY;
    this.pos3 = e.clientX;
    this.pos4 = e.clientY;
    this.setState({
      y: this.reff.current.offsetTop - this.pos2 + "px",
      x: this.reff.current.offsetLeft - this.pos1 + "px"
    });
  };

  closeDragElement = () => {
    document.onmouseup = null;
    document.onmousemove = null;
  };

  showMoreOptionsPopup = () => {
    this.setState({
      showMoreOptionsPopup: !this.state.showMoreOptionsPopup
    });
  };

render() {
    return (
      <div>
        {this.state.showMoreOptionsPopup && (
          <div
            id="more_options_popup"
            style={{
              left: this.reff.current.offsetLeft - 170 + "px",
              top: this.reff.current.offsetTop - 130 + "px"
            }}
          >
           Help Doc
          </div>
        )}

        <div
          id="more_options_button"
          onClick={this.showMoreOptionsPopup}
          style={{ left: this.state.x, top: this.state.y }}
          onMouseDown={this.dragMouseDown}
          ref={this.reff}
        >
          {this.state.showMoreOptionsPopup ? (
            <Icon name="close" />
          ) : (
            <Icon name="menu" />
          )}
        </div>
      </div>
    );
  }
}

You could measure the time that has passed since < mouse.press > and < mouse.release >.您可以测量自 <mouse.press> 和 <mouse.release> 以来经过的时间。
If it has been no longer than ~100ms then it probably was a "click" and not a "drag".如果它不超过〜100ms,那么它可能是“点击”而不是“拖动”。


You can also implement < mouse.position.change > with some < Δ position > (~10px)您还可以实现 < mouse.position.change > 与一些 <Δ position > (~10px)
(so that a slight movement of a cursor when pressing a button won't register as "drag"). (因此按下按钮时 cursor 的轻微移动不会注册为“拖动”)。


If you combine both of those factors, determining whether it was a "click" or a "drag" is easy.如果将这两个因素结合起来,确定是“点击”还是“拖动”很容易。

I would just, on click, set onClick to null and set onMouseUp to set onClick to whatever functionality you want:我只需单击一下,将onClick设置为null并设置onMouseUp以将onClick设置为您想要的任何功能:


<element onClick="" onMouseUp="this.onClick = <something>"></element>

暂无
暂无

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

相关问题 当我使用Raphael在像photoshop之类的div上拖动鼠标时如何绘制简单图形 - how to paint Simple graphics when i dragging my mouse on a div like photoshop using Raphael 我如何使函数仅在鼠标未悬停在指定的div上时运行? - How do I make a function to run only when the mouse is NOT hovering a specified div? 当我使用鼠标单击时,我的onClick函数起作用,但是当我使用键盘时,它却不起作用。 任何建议如何解决? - my onClick function is working when i use mouse click , but when i use the keyboard , it is not working . any suggestions how to fix that? 时间到了如何停止我的 onclick 功能? - How to stop my onclick function when the time is up? 如何在ASP.NET中使用jQuery使我的Div可拖动 - How I can make my Div draggable with jQuery in ASP.NET 当我向上滚动并在向上滚动或当我将鼠标悬停在上面时,如何使导航条变得透明? - How do i make my nav bar transparent when I scroll down and come back when I scroll up or when I hover my mouse over is? 如何使div对齐? - How do I make my div's lined up? 使一个 div 能够在单击时运行 onclick function - Make a div able to run onclick function when it is clicked ReactJS:为什么我的可拖动弹出功能组件在拖动时没有停留在鼠标下方? - ReactJS: Why isn't my draggable pop-up functional component staying under the mouse when dragged? 如何仅通过拖动&#39;#head&#39;div使&#39;#box&#39;可拖动,而当我拖动&#39;#content&#39;时不使&#39;#box&#39;可拖动 - how to make '#box' draggable only by drag the '#head' div , not make the '#box' draggable when i drag '#content'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM