最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you simulate a drag event (mousemove with a button clicked)? - Stack Overflow

programmeradmin1浏览0评论

I want to be able to trigger the event and have it show that there was actually a button that was down at the time the event was fired.

$('table').on('mousemove', function(e) {
  var is_dragging = e.buttons.length > 0;
  // is_dragging should === true;
});

$('table').trigger('mousemove'); // is_dragging will be false

I want to be able to trigger the event and have it show that there was actually a button that was down at the time the event was fired.

$('table').on('mousemove', function(e) {
  var is_dragging = e.buttons.length > 0;
  // is_dragging should === true;
});

$('table').trigger('mousemove'); // is_dragging will be false
Share Improve this question edited May 16, 2019 at 11:28 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 22, 2015 at 20:39 Dan P.Dan P. 1,4311 gold badge16 silver badges28 bronze badges 10
  • What is wrong with your current code? Please read minimal reproducible example. – Anders Commented Dec 22, 2015 at 20:41
  • Usually mousemove is attached in mousedown handler to emulate dragging, hence no need to check if "dragging" is on. Please explain in more details, what you actually need. – Teemu Commented Dec 22, 2015 at 20:44
  • 1 @Anders I updated the code to show the issue I'm having – Dan P. Commented Dec 22, 2015 at 20:54
  • @Teemu I want to be able to trigger the event and have it show that there was actually a button that was down at the time the event was fired. – Dan P. Commented Dec 22, 2015 at 20:55
  • 1 @DanielPatz Check my answer for the custom event. That should work – Sachin Commented Dec 22, 2015 at 21:11
 |  Show 5 more ments

3 Answers 3

Reset to default 1

Try this out. Creating a custom event and attaching buttons property to it.

$('table').on('mousemove', function(e) {
  var is_dragging = e.buttons.length > 0;
  // is_dragging should === true;
});

var e = $.Event( 'mousemove' );
e.buttons = ['<set any key you need>'];

$('table').trigger(e); 

So based on what i think you are trying to acplish. You want to have an event handler for onmousemove where you are checking to see if a button is down (doesn't matter which button is down) however you want to manually trigger the event and have the condition is_dragging result in true.

If the event is manually triggered using $('table').trigger('mousemove');the event will not have a buttons property however it will have a .isTrigger property which will == 3.

Try this: https://jsfiddle/SeanWessell/L2z0su3j/

$('table').on('mousemove', function(e) {
  var is_dragging = e.buttons > 0 || e.buttons == undefined && e.isTrigger == 3;
  // is_dragging should === true;
  $('p').html(is_dragging.toString())
});

$('#trigger').click(function(){

    $('table').trigger('mousemove');

})

::UPDATE::

look into ondragstart http://www.w3schools./jsref/event_ondragstart.asp

=======

*.buttons is for what button you have clicked. At the time of mousemove you have not clicked a button.

Meaning your e.buttons === 0

Documentation on MouseEvent.button: http://www.w3schools./jsref/event_button.asp

You can verify this by throwing a debugger in your function and inspecting your e variable

JSFiddle: http://jsfiddle/qf3u8m61/

发布评论

评论列表(0)

  1. 暂无评论