jQuery - How can I continue an animation while the mouse hovers over an element?

后端 未结 2 392
一生所求
一生所求 2020-12-30 08:56

I need a way to perform some kind of \'whileonmouseover\' function to continue an animation while the mouse overs over an element...

For example, given this function

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 09:36

    I would suggest to move the following part outside the scope of the $(document).ready() function:

    var doLoop = true;
    
    function doAlert()
    {
        if (!doLoop) return;
    
        alert(1);
        doAlert();
    }
    

    So try this code instead:

    var doLoop = true;
    
    function doAlert()
    {
        if (!doLoop) return;
    
        alert(1);
        doAlert();
    }
    
    $(document).ready(function()
    {
        $('#button').hover(function()
        {
            doAlert();
        }, function()
        {
            doLoop = false;
        });
    });
    

提交回复
热议问题