【问题标题】:jquery animate with image rotate doesn't work after first function call带有图像旋转的jquery动画在第一次函数调用后不起作用
【发布时间】:2017-03-07 14:45:25
【问题描述】:

函数调用存在问题我的转换图像旋转步骤在我的页面加载时第一次在初始函数调用中调用后不起作用。每次在我构建的图表上更改任何值时都会调用此函数。要画一幅画,有两个硬币从屏幕顶部掉落,然后旋转两次,然后落在一堆硬币上。取而代之的是,每次调用该函数时,硬币只会从屏幕顶部掉落/动画。

<div id="secondCoin"><img class="coin" src="./images/one_coin.png" alt="Second Coin Drops" /></div>
<div id="firstCoin"><img class="coin" src="./images/one_coin.png" alt="First Coin Drops" /></div>
<div id="showCoins"><img class="coin-stack" src="./images/fullstack_coins.png" alt="Stack of Coins" /></div>

function coinStack(height){
var coinSound = document.getElementById('coin-sound');
var rotateDeg = 720;
// prevents sound play on multiple clicks
coinSound.pause();
coinSound.currentTime = 0;
// causes coin stack to slide upward
$("#showCoins").css("display", "inline-block");
$("#showCoins").css("top", height + "px");
screenWidth <= 400 ? $("#showCoins").stop().animate({top: '3'},1000) : $("#showCoins").stop().animate({top: '0'},1000);
// first coin drops
$("#firstCoin").css("display", "inline-block");
$("#firstCoin").css("top", "-324px");
$("#firstCoin").clearQueue().delay(1000).animate({top: '10', deg: rotateDeg},{
    duration: 350, 
    step: function(now){
        $("#firstCoin").css({ transform: "rotate(" + now + "deg)" });
    }
});
// second coin drops
$("#secondCoin").css("display", "inline-block");
$("#secondCoin").css("top", "-324px");
$("#secondCoin").clearQueue().delay(1400).animate({top: '0', deg: rotateDeg},{
    duration: 350, 
    step: function(now){
        $("#secondCoin").css({ transform: "rotate(" + now + "deg)" });
    }
});
// coin sound effect
coinSound.volume = 1.0;
coinSound.play(); 

}

我尝试过使用 .stop()、.clearQueue(),以不同的方式设置/删除转换属性,并检查了 stackoverflow 上的各种方法。似乎没有任何效果,所以我希望另一双眼睛会发现我的问题。提前致谢!

【问题讨论】:

  • 您是否在控制台中遇到任何错误,您能否将 HTML 代码提供给我们以便我们试用?
  • 控制台上没有出现任何错误,我马上就会有 html。
  • 这里一切正常...你能再描述一下这个问题吗?我没有得到“相反的工作方式,硬币只会在每次调用函数时从屏幕顶部掉落/动画”部分。这就是所谓的行为......
  • 所以页面第一次加载并调用该函数时,硬币从屏幕顶部掉落并旋转两次没有问题。只要在我的图表中更改任何值,就会调用 coinStack 函数,硬币将从屏幕顶部掉落,但不会再旋转。它们只在第一次旋转时使用 transform:rotate(720deg)。

标签: javascript jquery jquery-animate css-transforms


【解决方案1】:

这可能有点晚了,但您需要为每次迭代在动画中添加另外两个旋转。这将解决您的问题,您可以一遍又一遍地调用相同的函数。

var rotateDeg = 720; //Declare this outside the function

$('button').click(function() {
  coinStack(400);
})

function coinStack(height) {

  $("#showCoins").css("display", "inline-block");
  $("#showCoins").css("top", height + "px");
  $("#firstCoin").css("display", "inline-block");
  $("#firstCoin").css("top", "-324px");
  
  $("#firstCoin").clearQueue().delay(1000).animate({
    top: '10',
    deg: rotateDeg
  }, {
    duration: 3500,
    step: function(now) {
      $("div").css({
        transform: "rotate(" + now + "deg)"
      });
    }
  });
  
  rotateDeg += 720; //For every animation you add another 720 degrees
};

$("#secondCoin").css("display", "inline-block");
$("#secondCoin").css("top", "-324px");
$("#secondCoin").clearQueue().delay(1400).animate({top: '0', deg: rotateDeg},{
    duration: 350, 
    step: function(now){
        $("#secondCoin").css({ transform: "rotate(" + now + "deg)" });
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="secondCoin"><img class="coin" src="#" alt="Second Coin Drops" /></div>
<div id="firstCoin"><img class="coin" src="#" alt="First Coin Drops" /></div>
<div id="showCoins"><img class="coin-stack" src="#" alt="Stack of Coins" /></div>

<button>Push to rotate</button>

【讨论】:

  • 聪明人,佐肯。仍然有点难以置信的是,每次调用函数时增加旋转是“允许”它旋转的原因。特别是因为我已经使用了至少 4 种不同的方法来更改/重置 transform 属性的度数值,但如果它有效,我会接受它。谢谢!
  • 因为您正在执行两个完整的旋转,所以您可以再添加两个(+720 度)或使用 $(your element).removeAttr('style') 从元素中删除 stile 以重置函数中的 now 变量。
  • 不幸的是,这是我用来尝试解决问题的方法之一。我没有成功尝试改变甚至完全删除这种风格。我很满意你的想法最终成功了,而所有其他尝试都失败了。再次感谢!
【解决方案2】:

无论页面的状态如何,您似乎都在执行代码。

尝试在其中插入您的代码:

$(document).ready(function(){
//your code here
})

有关详细信息,请参阅: https://learn.jquery.com/using-jquery-core/document-ready/

【讨论】:

  • 这不是问题。我有一个在 $(document).ready 中调用的函数,另一个函数调用它,另一个调用它的函数像这样调用它。初始化未来值(); --> getFutureValue(); --> coinStack(barHeight);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多