javascript - jQuery 阻止 onmouseover 事件

标签 javascript jquery

我已经搜索了一段时间,但还没有结果..我有多个带有动画的鼠标悬停事件。因此,如果仍有其他动画正在运行,我希望能够禁用鼠标悬停事件的发生。

我已经玩了很多 jQuery.stop() 但不可能让它工作。我实际上不确定将该代码放在我的函数中的何处..

这是我的带有 map 的 html:

<img src="assets/menu588x588.png" alt="" usemap="#map" />
   <map id="map" name="map">
      <area onmouseover="driveTo(330);" shape="poly" coords="93, 264, 2, 270, 7" />
      <area onmouseover="driveTo(300);" shape="poly" coords="122, 336, 101, 390" />
      <area onmouseover="driveTo(230);" shape="poly" coords="370, 335, 446, 388, 3574" />
   </map>

我的 JS 函数有一些超时和动画:

    function driveTo(new_position){
              $(function() {
                var pre_position   = Number(document.getElementById("pre-position").value);
                var goal           = 360 - pre_position + new_position;
                var $elem          = $(".car-box"), degree = pre_position, timer;

                rotate(goal);

                function rotate(goal) {
                  $elem.css({ 'transform': 'rotate(' + degree + 'deg)'});

                  if(new_position < pre_position){
                    if(goal > 0){
                      timer = setTimeout(function() {
                          --goal;
                          ++degree;
                          rotate(goal);
                      },5);
                    }
                    if(goal === 0){
                      document.getElementById("pre-position").value = new_position;
                    }
                  }
                }
              });
            }

我担心在所有这些内部功能中有点迷失..

提前非常感谢您的任何建议!

最佳答案

试试这个:

var inProgress = false;
function driveTo(new_position){

    if(inProgress){
        return;
    }


    $(function() {
        inProgress = true;

        var pre_position   = Number(document.getElementById("pre-position").value);
        var goal           = 360 - pre_position + new_position;
        var $elem          = $(".car-box"), degree = pre_position, timer;

        rotate(goal);

        function rotate(goal) {
            $elem.css({ 'transform': 'rotate(' + degree + 'deg)'});

            if(new_position < pre_position){

                if(goal > 0){
                    timer = setTimeout(function() {
                        --goal;
                        ++degree;
                        rotate(goal);
                    },5);
                }
                if(goal === 0){
                    document.getElementById("pre-position").value = new_position;
                    inProgress = false;
                }
            }
        }
    });
}

关于javascript - jQuery 阻止 onmouseover 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32434011/

相关文章:

javascript - 如何为node.js指定Jasmine源文件?

javascript - 在js api中获取facebook好友列表

javascript - 每 X 秒自动刷新一些项目列表

javascript - 做出选择后关闭移动菜单

javascript - 使用JQuery修改某个ID的HTML元素

javascript - 什么是 JavaScript 中好的数学集实现?

javascript - 使用js在选择时获取所选项目

javascript - 我的表单验证无法正常工作

javascript - 使用 jQuery 循环遍历容器 div 内的所有 div

javascript - 如何计算页面上具有值的输入总数?