An “if mouseover” or a “do while mouseover” in JavaScript/jQuery

馋奶兔 提交于 2019-12-17 16:34:35

问题


Is there a JavaScript or jQuery solution to run a function repeatedly (after setTimeout) while the mouse is over a DOM object? Otherwise said, is there a JavaScript "do while mouseover" (or "if mouseover")?

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

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

    });

回答1:


$('someObject').on('mouseenter', function() {
    this.iid = setInterval(function() {
       // do something           
    }, 25);
}).on('mouseleave', function(){
    this.iid && clearInterval(this.iid);
});

Example Look here




回答2:


I would solve this issue using the onmouseout event. Start whatever you intended to do while the mouse is over the specified component on the mouseover event. When onmouseout event occurs i would stop it.




回答3:


i use new bind style of jQuery.

$(el).bind({
'mouseenter': function(){console.log('Mouse over');}, 
'mouseleave': function(){console.log('Mouse leave');}
});



回答4:


I know this is kind of old, but I think the proper function is already in JavaScript, onmousemove does just that.




回答5:


OBJ.addEventListener("mouseenter", function() {
  focus=true;
});
OBJ.addEventListener("mouseleave", function() {
  focus=false;
});

Just in case you don't want to use jquery you can use this :)



来源:https://stackoverflow.com/questions/3966273/an-if-mouseover-or-a-do-while-mouseover-in-javascript-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!