悬停的 jQuery 动画
当您将鼠标悬停在锚元素上时,我正在尝试为徽标添加动画效果。当我将鼠标悬停在徽标上时,第一个图像完成后,图像需要向上移动,另一个图像需要向下移动。当鼠标离开时,反之亦然,当前元素(第二个)应该向上,当鼠标离开时,第一个元素应该向下。
我知道如何设置动画,但是当我快速悬停在锚元素上方时,我遇到了麻烦。有时两个图像(第一个和第二个出现)等。
这是我已经得到的,我应该如何正确执行此操作?
function logoAnimation() {
var logo = jQuery('#logo a');
var house = jQuery('#logo_icon_house');
var brush = jQuery('#logo_icon_brush');
var inHandler = function () {
logo.unbind('mouseleave', outHandler);
brush.animate({
"top": "-27px"
}, 250, function(){
house.animate({
"top": "42px"
}, 250,function(){
logo.bind('mouseleave', outHandler);
});
}
);
}
var outHandler = function () {
logo.unbind('mouseenter', inHandler);
house.animate({
"top": "-21px"
}, 250, function () {
brush.animate({
"top": "39px"
}, 250,function(){
logo.bind('mouseenter', inHandler);
});
});
}
logo.mouseenter(inHandler).mouseleave(outHandler);
}
在正确回答我的问题后,我设法使用以下代码解决了这个问题:
function logoAnimation() {
var logo = jQuery('#logo a');
var house = jQuery('#logo_icon_house');
var brush = jQuery('#logo_icon_brush');
var inHandler = function () {
if(brush.is(':animated')) {
return;
}
brush.stop(true,true).animate({
"top": "-27px"
}, 250, function(){
if(house.is(':animated')) {
return;
}
house.animate({
"top": "42px"
}, 250);
}
);
}
var outHandler = function () {
house.stop(true,true).animate({
"top": "-21px"
}, 250, function () {
if(brush.is(':animated')) {
return;
}
brush.animate({
"top": "39px"
}, 250);
});
}
logo.mouseenter(inHandler).mouseleave(outHandler);
}
I'm trying to animate a logo when you hover over the anchor element. When I hover over the logo, an image needs to go upwards and another one downwards when the first one is done. When the mouse leaves it should the other way around, the current element (2nd) should go upwards and when it's gone the first element should go downwards.
I know how to animate this, however I'm having troubles when I hover really fast above my anchor element. Sometimes both images (1st & 2nd appear) etc.
this is what I already got, how should I do this properly?
function logoAnimation() {
var logo = jQuery('#logo a');
var house = jQuery('#logo_icon_house');
var brush = jQuery('#logo_icon_brush');
var inHandler = function () {
logo.unbind('mouseleave', outHandler);
brush.animate({
"top": "-27px"
}, 250, function(){
house.animate({
"top": "42px"
}, 250,function(){
logo.bind('mouseleave', outHandler);
});
}
);
}
var outHandler = function () {
logo.unbind('mouseenter', inHandler);
house.animate({
"top": "-21px"
}, 250, function () {
brush.animate({
"top": "39px"
}, 250,function(){
logo.bind('mouseenter', inHandler);
});
});
}
logo.mouseenter(inHandler).mouseleave(outHandler);
}
after the correct answer to my question, I managed to work this out with the following code:
function logoAnimation() {
var logo = jQuery('#logo a');
var house = jQuery('#logo_icon_house');
var brush = jQuery('#logo_icon_brush');
var inHandler = function () {
if(brush.is(':animated')) {
return;
}
brush.stop(true,true).animate({
"top": "-27px"
}, 250, function(){
if(house.is(':animated')) {
return;
}
house.animate({
"top": "42px"
}, 250);
}
);
}
var outHandler = function () {
house.stop(true,true).animate({
"top": "-21px"
}, 250, function () {
if(brush.is(':animated')) {
return;
}
brush.animate({
"top": "39px"
}, 250);
});
}
logo.mouseenter(inHandler).mouseleave(outHandler);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在调用
.animate()
(在链内)之前调用.stop(true, true)
,或者您可以创建一些类似的语句。这可以确保在开始新的动画之前,所有动画都在画笔上停止。
.stop()
的参数是“CleartQueue”和“JumpToEnd”。这确保了只有当前没有动画正在进行时
.animate()
才会被调用。参考:.stop(),:动画
You either should call
.stop(true, true)
before you call.animate()
(within the chain) or you could create some statement like.That makes sure all animations are stopped on
brush
before starting a new one. The parameters of.stop()
are "CleartQueue" and "JumpToEnd".That makes sure,
.animate()
only gets called if no animation currently is going on.Ref.: .stop(), :animated