简体   繁体   English

将mousemove()与click事件绑定

[英]bind mousemove() with click event

I would like to re-attach mousemove() after a click event is fired. 我想在触发click事件后重新附加mousemove()。 I am using jquery off/on to handle this functionality. 我使用开/关jQuery来处理此功能。 The mousemove is turned off when a html element is displayed. 显示html元素时,mousemove将关闭。 The on is invoked when the user clicks a button. 用户单击按钮时将调用on。 The off() works as expected, but I am not successfully reattaching the event when the button is clicked. off()可以正常工作,但是单击按钮时,我没有成功重新连接事件。

$(document).ready(function() {
  $(this).mousemove(function(e) {
    console.log("The mouse moved");
  });  
});

function buttonEvents(){
  $('.reset').click(function() {
    modal.hide();
    $(document).on('mousemove');
    console.log('The mouse moved.');
  });
  $('.close').click(function() {
    modal.hide();  
  });

  if($('.photoCnt').css('display') === 'block'){
    $(document).off('mousemove');
  }
}

I tired wrapping the mousemove() into a function and calling it, but it is not working. 我厌倦了将mousemove()包装到一个函数中并调用它,但是它不起作用。

function userActivity(){
  $(this).mousemove(function(e) {
    console.log("The mouse moved");
  });

  function buttonEvents(){
    $('.reset').click(function() {
      modal.hide();
      $(document).on('mousemove', userActivity);
      console.log('The mouse moved.');
    });
    $('.close').click(function() {
      modal.hide();  
    });

    if($('.photoCnt').css('display') === 'block'){
      $(document).off('mousemove', userActivity);
    }
  }
}

What is the correct way to accomplish this functionality? 什么是完成此功能的正确方法?

If you find something isn't working, my advice is to separate your desired actions into smaller functions. 如果您发现某些问题不起作用,我的建议是将所需的操作分成较小的功能。 You can then test those functions individually and then combine them together to achieve your desired result. 然后,您可以分别测试这些功能,然后将它们组合在一起以达到所需的结果。

For example, I would create 3 functions, addMouseMoveListener() , removeMouseMoveListener() , and onMouseMoveHandler(ev) . 例如,我将创建3个函数, addMouseMoveListener()removeMouseMoveListener()onMouseMoveHandler(ev)

function addMouseMoveListener() {
    $(document).on('mousemove', onMouseMoveHandler);
    console.log("addMouseMoveListener() finished");
}

function removeMouseMoveListener() {
    $(document).off('mousemove', onMouseMoveHandler);
    console.log("removeMouseMoveListener() finished");
}

function onMouseMoveHandler(ev) {
    console.log("Mouse moved!", ev);
}

After testing each of these individual functions, you can organize them in your code to be called at the appropriate times. 测试完各个功能后,您可以在适当的时间将它们组织到代码中以进行调用。

$(document).ready(function() {
    // initialize your button event handlers
    // turn on/off mousemove event handlers when desired

    $('.reset').click(function() {
        modal.hide();
        addMouseMoveListener();
    });
    $('.close').click(function() {
        modal.hide();
        removeMouseMoveListener();
    });

});

Note: I left out the code for your checking the modal's CSS. 注意:我省略了用于检查模式的CSS的代码。 Instead, you may be able to add/remove the mousemove event handler at the same time that you hide/show the modal element. 相反,您可以在隐藏/显示模式元素的同时添加/删除mousemove事件处理程序。 You could ignore this suggestion and put your CSS check code inside your onMouseMoveHandler(ev) method, but you might experience a performance issue since mousemove events tend to fire very often. 您可以忽略此建议,而将CSS检查代码放入onMouseMoveHandler(ev)方法中,但是由于mousemove事件往往会频繁触发,因此您可能会遇到性能问题。

If i consider your idea right, example to change/revoke clickevents: Here is the copepen ( http://codepen.io/f7o/pen/jrBrWx ) 如果我认为您的想法正确,请更改/撤消clickevents的示例:这是copepen( http://codepen.io/f7o/pen/jrBrWx

on load: console logs mouse move on click on test link: mouse move isn't tracked anymore 加载时:控制台记录单击测试链接时的鼠标移动:不再跟踪鼠标移动

If your using jQuery make sure you call the buttonEvents function on clicking some button $('#someButton').click(...) 如果您使用的是jQuery,请确保在单击某些按钮$('#someButton').click(...)调用buttonEvents函数

In your code mistake - userActivity is circular callback. 在您的代码错误中-userActivity是循环回调。 Each move this born new move listners. 每一个举动都诞生了新的举动列表器。 Change to: 改成:

$(document).ready(function() {
    var userActivity = function() {
        console.log("The mouse moved");
    }
    $(document).on('mousemove', userActivity);
    $('.reset').click(function() {
        modal.hide();
        $(document).on('mousemove', userActivity);
        console.log('The mouse moved.');
    });
    $('.close').click(function() {
        modal.hide();
    });
    if ($('.photoCnt').css('display') === 'block') {
        $(document).off('mousemove', userActivity);
    }
});

Hope, it works :) 希望,它有效:)

The .on() function expects at least two arguments. .on()函数至少需要两个参数。 The first is the name of the event you want to listen to and the second is the function which should be invoked whenever the event fired. 第一个是您要侦听的事件的名称,第二个是在触发事件时应调用的函数。 (See http://api.jquery.com/on/ ) (请参阅http://api.jquery.com/on/

Therefore $(document).on('mousemove') does not install any event handler in your first example. 因此,第一个示例中的$(document).on('mousemove')不会安装任何事件处理程序。

One solution is to name your event handler. 一种解决方案是命名事件处理程序。

function myEventHandler() {
  document.log("The mouse moved");
}
// ...
$(this).mousemove(myEventHandler);
// ...
$(document).on('mousemove', myEventHandler);

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

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