更改 animate() 速度,同时将其动画化并将鼠标悬停在元素上

change animate() speed while it animates and hover over element

本文关键字:鼠标 悬停 元素 动画 速度 更改 animate      更新时间:2023-09-26

我需要更改使用 jQuery animate() 动画的元素的速度。仅当用户将鼠标悬停在给定元素上时,速度才应更改。这是我到目前为止所拥有的,但它似乎没有任何作用。

    var speed = 2000;
    $('li').hover(
    function(){
        var speed = 500;
    }
    );
    $(function () {
       function drive() {
         $( ".plane" ).animate({
            "left": "+=50",
          }, speed, drive);
        } 
      drive();
    });

您需要修改全局变量速度,而不仅仅是在悬停函数回调中设置另一个局部变量。另请注意,悬停将执行两次,一次用于悬停在内,另一次用于悬停在外。

   var speed = 2000;
    $('li').hover(function(){
        speed += 500;
    });
    $(function () {
       function drive() {
         $( ".plane" ).animate({
            "left": "+=50",
          }, speed, drive);
        } 
      drive();
    });