Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

javascript - 'onmousedrag'事件js('onmousedrag' event js)

I have some code that works each time onmouseclick and continuously onmousemove when I set them accordingly.

(我有一些代码每次在onmouseclick上都有效,并在相应地进行设置时不断onmousemove起作用。)

I am looking for a way to combine the two (ie like a click and drag) which I thought would be a simple task but cannot find any simple explanation.

(我正在寻找一种将两者结合起来的方法(例如单击和拖动),我认为这是一个简单的任务,但找不到任何简单的解释。)

The closest I get is drag-and-drop tutorials.

(我得到的最接近的是拖放教程。)

Do I have to call the onmousemove event while the onmouseclick event is triggered?

(在触发onmouseclick事件时,我是否必须调用onmousemove事件?)

Or is there something really simple I am completely overlooking?

(还是我完全忽略了一些真的很简单的事情?)

  ask by myol translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You might want to just use a flag like this: http://jsfiddle.net/n3MeH/ .

(您可能只想使用这样的标志: http : //jsfiddle.net/n3MeH/ 。)

var isMouseDown = false;
document.onmousedown = function() { isMouseDown = true  };
document.onmouseup   = function() { isMouseDown = false };
document.onmousemove = function() { if(isMouseDown) { /* do drag things */ } };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...