当mouseDown按下js时如何实现mousemove [英] how to implement mousemove while mouseDown pressed js

查看:361
本文介绍了当mouseDown按下js时如何实现mousemove的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有在按下鼠标时才需要实现鼠标移动事件。

I have to implement mouse move event only when mouse down is pressed.

我只需要在鼠标按下和鼠标移动时执行OK Moved。

I need to execute "OK Moved" only when mouse down and mouse move.

我用过这段代码

 $(".floor").mousedown(function() {
  $(".floor").bind('mouseover',function(){
      alert("OK Moved!");
  });
})
.mouseup(function() {
 $(".floor").unbind('mouseover');
});


推荐答案

使用 mosemove event。

来自 mousemove mouseover jquery docs:

From mousemove and mouseover jquery docs:


mousemove 事件是鼠标指针在元素内移动时发送到元素。

The mousemove event is sent to an element when the mouse pointer moves inside the element.

鼠标悬停事件发送到元素时鼠标指针进入元素。

The mouseover event is sent to an element when the mouse pointer enters the element.

示例:(检查控制台输出)

Example: (check console output)

$(".floor").mousedown(function () {
    $(this).mousemove(function () {
        console.log("OK Moved!");
    });
}).mouseup(function () {
    $(this).unbind('mousemove');
}).mouseout(function () {
    $(this).unbind('mousemove');
});

https://jsfiddle.net/n4820hsh/

这篇关于当mouseDown按下js时如何实现mousemove的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆