启动/停止函数执行 jQuery/JS 在鼠标事件上

Start/Stop Function Execution jQuery/JS on mouse event

本文关键字:鼠标 JS 事件 执行 函数 启动 jQuery      更新时间:2023-09-26

我正在使用此脚本为div的背景渐变设置动画:http://codepen.io/quasimondo/pen/lDdrF

这是我想要实现的目标:- 悬停时运行函数以动画渐变背景- 在鼠标退出时停止动画或暂停

在 CodePen 上,我用这个替换了第 61 行:

$('#logoCA').hover( function(){
      var gradient = setInterval(updateGradient,2);
    },
    function(){
      clearInterval(gradient);
    });

由于某种原因不起作用。有什么想法或更好的方法吗?

非常感谢。

所以这是最终版本:

var gradient;
  $('#logoCA').hover( function(){
      gradient = setInterval(updateGradient,2);
      gradient;
    },
    function(){
      clearInterval(gradient);
    });