延迟动画,直到其他动画完成(滑动内容(
Delay animation until other animation is complete (sliding content(
我有这样的代码,它可以在div之间滑动动画。如果一个项目被点击,它的相关内容就会滑出。如果单击另一个项目,则当前内容滑回,新内容滑出。
然而,
var lastClicked = null;
var animateClasses = ['ale', 'bramling', 'bullet', 'miami-weisse'];
for (var i=0; i<animateClasses.length; i++) {
(function(animCls) {
$('.each-brew.'+animCls).toggle(function() {
if (lastClicked && lastClicked != this) {
// animate it back
$(lastClicked).trigger('click');
}
lastClicked = this;
$('.each-brew-content.'+animCls).show().animate({ left: '0' }, 1000).css('position','inherit');
}, function() {
$('.each-brew-content.'+animCls)
.animate({ left: '-33.3333%' }, 1000, function() { $(this).hide()}) // hide the element in the animation on-complete callback
.css('position','relative');
});
})(animateClasses[i]); // self calling anonymous function
}
然而,一旦已经打开的内容滑回,滑出的内容就会滑出得太快——它需要等到内容完全滑回后才能滑出。这可能吗?
这里有一个链接,指向我目前正在进行的工作,以获得一个想法(http://goo.gl/s8Tl6)。
提前干杯,R
以下是我对它的看法,它是一个没有标记更改的替代品。当点击菜单项时,您希望发生以下三件事之一:
- 如果单击的项目当前正在显示,请将其隐藏
- 如果显示其他内容,请将其隐藏,然后显示当前项目的内容
- 如果没有显示任何内容,则显示当前项目的内容
var lastClicked = null;
// here lastClicked points to the currently visible content
var animateClasses = ['ale', 'bramling', 'bullet', 'miami-weisse'];
for (var i=0; i<animateClasses.length; i++) {
(function(animCls) {
$('.each-brew.'+animCls).click(function(event){
if(lastClicked && lastClicked == animCls){
// if the lastClicked is `this` then just hide the content
$('.each-brew-content.'+animCls).animate(
{ left: '-33.3333%' }, 1000,
function() {
$(this).hide();
}).css('position','relative');
lastClicked = null;
}else{
if(lastClicked){
// if something else is lastClicked, hide it,
//then trigger a click on the new target
$('.each-brew-content.'+lastClicked).animate(
{ left: '-33.3333%' }, 1000,
function() {
$(this).hide();
$(event.target).trigger('click');
}).css('position','relative');
lastClicked = null;
}else{
// if there is no currently visible div,
// show our content
$('.each-brew-content.'+animCls).show()
.animate({ left: '0' }, 1000)
.css('position','relative');
lastClicked = animCls;
}
}
});
})(animateClasses[i]); // self calling anonymous function
}
好吧,我很确定还有其他更简单的可能性,我没有太多时间,但这里有一个正在工作的jsfiddle:http://jsfiddle.net/uaNKz/
基本上,您可以使用callback
函数来等待动画完成。在这种特殊情况下,它是complete: function(){...}
$("document").ready(function(){
$('#ale').click(function(){
if ($('div').hasClass('toggled')){
$('.toggled').animate({ width: "toggle" }, { duration:250, complete: function(){
$('#alecont').animate({ width: "toggle" }, { duration:250 }).addClass('toggled');}
}).removeClass('toggled');
}else{
$('#alecont').animate({ width: "toggle" }, { duration:250 }).addClass('toggled');
}
});
$('#bramling').click(function(){
if ($('div').hasClass('toggled')){
$('.toggled').animate({ width: "toggle" }, { duration:250, complete: function(){
$('#bramcont').animate({ width: "toggle" }, { duration:250 }).addClass('toggled');}
}).removeClass('toggled');
}else{
$('#bramcont').animate({ width: "toggle" }, { duration:250 }).addClass('toggled');
}
});
});
如果一个div被展开,我会给出一个toggled
类。由于你页面上的动画似乎很坏,我认为这将是一个更好的方法。但请记住:我的代码不是很好。只要速度快,就可以重构。它正在工作。。
与其使用切换,不如将一个on"click"处理程序绑定到您的".each brew"div。在处理程序中,首先隐藏内容div,然后在动画完成时显示适当的内容。您可以通过承诺或回调来实现这一点。像这样的。。。
$(".each-brew").on("click", function (event) {
$(".each-brew-content").show().animate({ left: "0" }, 1000, function() {
// Get the brew name from the class list.
// This assumes that the brew is the second class in the list, as in your markup.
var brew = event.currentTarget.className.split(/'s+/)[1];
$(".each-brew-content." + brew).animate({ left: "-33.3333%" }, 1000, function() { $(this).hide(); });
});
});
我认为一个事件和观察者会帮你完成任务。
在动画完成时设置回调函数以触发事件。
侦听器将首先侦听任何动画事件,并且在该事件被触发之后侦听完成事件。当完成事件被激发时,执行初始动画事件.run方法(或您想称之为的任何方法)
在听众内部在newanimationeventtriger(newanim)上等待x秒(以消除无限循环poss),而如果此lastevent触发done=true{new_anim.run();}
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- 对象获胜'如果qml中的其他地方定义了数字动画属性,则t设置动画
- jQuery悬停动画只在其他类似元素中的一个元素上
- 将列表项设置为负顶部的动画会使其他列表项下降
- 在页面加载的所有其他文件之后加载动画 gif
- 在单击时对元素进行动画处理,并在该元素和其他元素上单击鼠标输入/鼠标离开
- 单击图像,隐藏其他图像并使用JQUERY对图像滑动进行动画处理
- 在JavaScript中缓和动画,而无需jQuery或其他库
- jQuery:如果其他滚动函数,则进行动画处理
- 在 SVG 动画中围绕舞台原点以外的其他位置旋转路径
- 动画折叠移动其他元素
- 与其他变换函数异步制作不透明度动画
- jQuery动画在FF中运行良好,但在所有其他浏览器中效果不佳
- Jquery 鼠标悬停在与其他动画函数冲突的移动上
- Mozilla Firefox或除Chrome以外的其他浏览器的缩放动画
- 如何在调用window.open()时显示进度条或任何其他动画
- 我如何使那些由于附近的其他元素调用$. fadein()而移动的元素产生动画?
- Javascript动画:IE vs其他浏览器
- 如何在隐藏其他影响其定位的元素后对元素进行动画
- 当鼠标悬停在显示子项目和动画其他项目时