无需鼠标移动即可激活Jquery mousemove() [英] Jquery mousemove() gets activated without a mouse movement

查看:132
本文介绍了无需鼠标移动即可激活Jquery mousemove()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做类似淡入的google.com(9月我想淡出文本)

I am trying to do a google.com like fade in (Cept i want to fade out text)

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
  $(document).ready(function() {
    $("html").mousemove(function () {
      $("p").fadeOut("slow");
    });
  });
</script>

使用该代码,即使我没有移动鼠标,淡出也会自动激活.发生在所有浏览器中.有提示吗?

With that code, my fade out gets automatically activated although I have not moved the mouse. Happens in all browsers. Any tips?

推荐答案

由于该事件最初会触发一次,并且每次将像素移动一次都会触发mousemove,因此您可以忽略第一个事件(可能是自动事件,具体取决于浏览器) )mousemove事件以获取所需的效果,如下所示:

Since the event fires once initially and mousemove fires every time you move it a pixel, you could just ignore the very first (possibly automatic, depending on browser) mousemove event to get the effect you want, like this:

$(function() {
  var moveCount = 0;
  $("html").mousemove(function () {
    if(moveCount++ === 0) return; //first run?
    $("p").fadeOut("slow");
    $(this).unbind('mousemove'); //unbind this, no need to stick around
  });
});​

您可以在此处尝试演示,我们所做的只是忽略了 首先触发mousemove事件,此后我们进行淡入淡出并取消绑定此处理程序,以使其在以后的mousemove触发中不再运行,只需清除即可.

You can try a demo here, all we're doing is ignoring the very first firing of the mousemove event, after that we do the fade and unbind this handler so that it doesn't run for future mousemove firings, just cleaning up.

这篇关于无需鼠标移动即可激活Jquery mousemove()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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