“如果鼠标悬停”或“在鼠标悬停时做”在JavaScript / jQuery中 [英] An "if mouseover" or a "do while mouseover" in JavaScript/jQuery

查看:127
本文介绍了“如果鼠标悬停”或“在鼠标悬停时做”在JavaScript / jQuery中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当鼠标在DOM对象上时,是否有JavaScript或jQuery解决方案重复运行功能(在 setTimeout 之后)?否则说,是否有JavaScript做鼠标悬停(或如果鼠标悬停)?

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);

    });


推荐答案

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

示例在这里

这篇关于“如果鼠标悬停”或“在鼠标悬停时做”在JavaScript / jQuery中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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