【问题标题】:An "if mouseover" or a "do while mouseover" in JavaScript/jQueryJavaScript/jQuery 中的“if mouseover”或“do while mouseover”
【发布时间】:2019-01-02 15:27:38
【问题描述】:

当鼠标悬停在 DOM 对象上时,是否有 JavaScript 或 jQuery 解决方案可以重复运行函数(在setTimeout 之后)?否则说,是否有 JavaScript “do while mouseover”(或“if mouseover”)?

    $('someObject').bind('mouseover', function() {

        //Do the following while mouseover 
        $('someOtherObject').css('margin-left',adjustedLeft + 'px');
        setTimeout(/*do it again*/,25);

    });

【问题讨论】:

    标签: javascript jquery dom mouseevent


    【解决方案1】:
    $('someObject').on('mouseenter', function() {
        this.iid = setInterval(function() {
           // do something           
        }, 25);
    }).on('mouseleave', function(){
        this.iid && clearInterval(this.iid);
    });
    

    Example Look here

    【讨论】:

    • 我使用新的 jQuery 绑定样式
    • 每次悬停是否可以执行一次该事件?目前它将被执行多次,直到元素悬停。我只想这样做一次。
    • 拥有this.iid && clearInterval(this.iid) 还不够clearInterval 有什么意义?
    【解决方案2】:

    我会使用 onmouseout 事件来解决这个问题。 当鼠标悬停在鼠标悬停事件的指定组件上时,开始您打算执行的任何操作。 当 onmouseout 事件发生时,我会停止它。

    【讨论】:

      【解决方案3】:

      我使用新的 jQuery 绑定样式。

      $(el).bind({ 'mouseenter': function(){console.log('鼠标悬停');}, 'mouseleave': function(){console.log('鼠标离开');} });

      【讨论】:

        【解决方案4】:

        我知道这有点老了,但我认为正确的功能已经在 J​​avaScript 中,onmousemove 就是这样做的。

        【讨论】:

          【解决方案5】:
          OBJ.addEventListener("mouseenter", function() {
            focus=true;
          });
          OBJ.addEventListener("mouseleave", function() {
            focus=false;
          });
          

          如果你不想使用 jquery,你可以使用这个:)

          【讨论】:

            猜你喜欢
            • 2011-03-03
            • 1970-01-01
            • 2016-01-15
            • 2011-11-09
            • 1970-01-01
            • 1970-01-01
            • 2013-02-11
            • 2011-08-02
            相关资源
            最近更新 更多