【发布时间】:2018-08-15 01:42:22
【问题描述】:
我有一个对象,当单击它时,它将淡出屏幕,当单击另一个对象时,它将淡入屏幕。
一旦对象消失,属性 Visibility:hidden 将被应用,但在对象返回之前,可见性设置为可见,在动画播放之前。否则它只会在屏幕上弹出。
问题是,当你点击对象太快时,在之前的动画完成之前,之前的可见性并没有恢复,因此两个对象最终被隐藏了。
我提供了我当前的工作代码。只要您花时间单击对象,这将非常有效,但是如果单击太快就会中断。
$(document).ready(function(){
console.log("[debug] - Script loaded ...")
//////////////////////////////////////////////////
// Variables //
//////////////////////////////////////////////////
var allCards = $(".card")
var selectedCard
var animationEnded
var removeClasses = "animated fadeOutUp fadeInDown"
var pullCardOut = "selected-card animated fadeOutUp"
var putCardBack = "animated fadeInDown"
// Assign a unique ID Number to each card ...
allCards.attr('id', function(i) {
console.log("[debug] - Assigned " + (i+1) + " cards a unique ID.")
return 'card'+(i+1);
});
//////////////////////////////////////////////////
// Card Click //
//////////////////////////////////////////////////
allCards.click(function(event){
// Get the unique ID of which card was clicked and store it in "cardClicked" variable ...
var cardClicked = event.target.id;
var selectedCard = $("#" + cardClicked)
var animationEnded = "webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend"
var previouslySelectedCard = null
// Dump the clicked card to the console ...
// console.log("[debug] - cardClicked = "+cardClicked+" ...")
console.log("[debug] - selectedCard = " + selectedCard + " ... (WHAT?)")
console.log("[debug] - " + cardClicked + " was clicked");
// Find any current card that may already be the selected card,
// and remove the previously selected card, before selecting the new card.
if ($("#all-cards").find("div.selected-card").length !== 0) {
console.log("[debug] - Found a selected card")
// Find the card number of the previously clicked card
var previouslySelectedCard = $('.selected-card').attr('id');
console.log("#" + previouslySelectedCard + " was previously selected")
$("#" + previouslySelectedCard).css("visibility", "visible");
$("#" + previouslySelectedCard).removeClass(pullCardOut).addClass(putCardBack).one(animationEnded, function() {
});
} else {
console.log("[debug] - No cards currently selected")
}
//Step 2: Add the appropriate classes to fade the card up.
//selectedCard.removeClass("selectedCard "+removeClasses);
selectedCard.addClass(pullCardOut).one(animationEnded, function() {
selectedCard.css("visibility", "hidden");
});
//Step 3: Return the card to the holder, if anywhere else on the page is clicked.
// Remove all associated classes and reset the state back to a refreshed page.
});
});
我的代码在这里有一个工作模型:http://jsfiddle.net/swox9a0y/3/,这可能更容易理解发生了什么。
我的问题是,在初始淡出动画和可见性设置之前,如何禁用点击第二个对象?
【问题讨论】:
-
您可以在第一个动画完成时调用第二个动画作为回调。您还可以在函数中添加延迟作为参数。 Promise 和链是确保一个事件在另一个事件开始之前完成的另外两个选项。
-
api.jquery 上的示例,似乎只在 jquery 动画完成时涵盖 .promise() 。当我在我的代码上添加一个 promise 调用时,它会立即触发,而不是在 animate.css 完成时触发。你有没有使用 .promise() 没有 jquery 动画的例子?
标签: jquery html css onclick animate.css