悬停的 jQuery 动画

发布于 2024-09-15 00:30:46 字数 1843 浏览 5 评论 0原文

当您将鼠标悬停在锚元素上时,我正在尝试为徽标添加动画效果。当我将鼠标悬停在徽标上时,第一个图像完成后,图像需要向上移动,另一个图像需要向下移动。当鼠标离开时,反之亦然,当前元素(第二个)应该向上,当鼠标离开时,第一个元素应该向下。

我知道如何设置动画,但是当我快速悬停在锚元素上方时,我遇到了麻烦。有时两个图像(第一个和第二个出现)等。

这是我已经得到的,我应该如何正确执行此操作?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余厌 2024-09-22 00:30:46

您应该在调用 .animate() (在链内)之前调用 .stop(true, true) ,或者您可以创建一些类似的语句。

brush.stop(true, true).animate({}); // ...

这可以确保在开始新的动画之前,所有动画都在画笔上停止。 .stop() 的参数是“CleartQueue”和“JumpToEnd”。

if(!brush.is(':animated')) {}

这确保了只有当前没有动画正在进行时.animate() 才会被调用。

参考:.stop():动画

You either should call .stop(true, true) before you call .animate() (within the chain) or you could create some statement like.

brush.stop(true, true).animate({}); // ...

That makes sure all animations are stopped on brush before starting a new one. The parameters of .stop() are "CleartQueue" and "JumpToEnd".

if(!brush.is(':animated')) {}

That makes sure, .animate() only gets called if no animation currently is going on.

Ref.: .stop(), :animated

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文